无法将 CIImage 保存到相机胶卷 - Swift 3

Jon*_*ett 2 cocoa-touch ios swift swift3

我正在尝试将过滤器应用于 CIImage,它是从 AVCaptureSession 的像素缓冲区创建的,然后将其保存到相机胶卷。

这是我的代码

        let image = CIImage(cvPixelBuffer: pixelBuffer)

        let filter = CIFilter(name: "CISepiaTone")
        filter?.setValue(image, forKey: kCIInputImageKey)
        filter?.setValue(0.1, forKey: kCIInputIntensityKey)

        let newImage = UIImage(ciImage: (filter?.outputImage)!)

        print ("new image: \(newImage)")

        UIImageWriteToSavedPhotosAlbum(newImage, self,    #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)
Run Code Online (Sandbox Code Playgroud)

这不会引发任何错误,并且当 image(_:didFinishSavingWithError:contextInfo:) 被调用时它没有错误

但是图像没有出现在我的库中。非常混淆!任何帮助表示赞赏。

谢谢。

小智 7

I've also faced this problem with same situation and solved it.

My method is: first convert it to CGImage, then convert the CGImage object to UIImage, in this case can you succeed to save the processed image to camera roll.

Here is the code.

let input = CIImage(image: MyImageView.image!)
let filter = PinkyFilter() // My custom filter
filter.setValue(input, forKey: kCIInputImageKey)
let context = CIContext() // Prepare for create CGImage
let cgimg = context.createCGImage(filter.outputImage, from: filter.outputImage.extent)
let output = UIImage(cgImage: cgimg!)
Run Code Online (Sandbox Code Playgroud)

Then the output is a save-able object.