Jonathan Plackett
Jonathan Plackett

Reputation: 2436

Cannot save CIImage to camera roll - Swift 3

I'm trying to apply a filter to a CIImage, which was created from a pixel buffer from an AVCaptureSession and then save it to the camera roll.

Here's my code

        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)

This throws no errors, and when image(_:didFinishSavingWithError:contextInfo:) is called it has not error

But the image does not appear in my library. Very confusing! Any help appreciated.

Thanks.

Upvotes: 4

Views: 1877

Answers (1)

Jerry Chou
Jerry Chou

Reputation: 91

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!)

Then the output is a save-able object.

Upvotes: 9

Related Questions