我如何将这个转换后的 UIImage 上传到 Firebase 存储?

问题描述

我需要将使用 AV Camera Foundation 拍摄的 UI 图像转换为黑白图像,因此在用户“批准”照片后,我调用了此函数

func convertToGrayScale(with originalImage:UIImage,imageStyle:String) -> UIImage {
        let currentFilter = CIFilter(name: imageStyle)
        currentFilter!.setValue(CIImage(image: originalImage),forKey: kCIInputimageKey)
        let output = currentFilter!.outputimage
        let context = CIContext(options: nil)
        let cgimg = context.createCGImage(output!,from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        return processedImage
    }

在这代码中:

// Save Image to Camera Roll
    @IBAction func saveButton(_ sender: Any) {
        let newImage = convertToGrayScale(with: image!,imageStyle: "CIPhotoEffectNoir")
        let imagetoSave = newImage
        UIImageWritetoSavedPhotosAlbum(imagetoSave,nil,nil)
        uploadPhoto()
        // downloadPhoto()
        dismiss(animated: true,completion: nil)
    }

用户批准照片时触发。

现在的问题是,虽然图像保存到相机胶卷时是黑白的,但上传到存储时显然不是,因为上传功能(如下所示)传递了未转换的图像进入存储:

// Upload to Firebase Storage
    func uploadPhoto() {
        let imageName = NSUUID().uuidString
        let storageRef = Storage.storage().reference().child(MyKeys.imagesFolder).child("\(imageName)")
        
        if let imageData = image!.jpegData(compressionQuality: 1) {
            storageRef.putData(imageData,Metadata: nil,completion: { (Metadata,error) in
            
                if error != nil {
                    print(error?.localizedDescription as Any)
                return
                }
                print(Metadata as Any)
                
        })
        }
        
        else {
            self.present(alertVC,animated: true,completion: nil)
            return
        }
        }

我尝试取出函数,直接将代码粘贴到@IBA Action函数中,但是当我尝试在手机上运行它时,我什至无法将图像保存到相机胶卷甚至存储中。 我应该如何修改功能以将这张黑白图像保存到存储中?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)