Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 436
Jonathan Plackett Avatar asked Oct 16 '25 16:10

Jonathan Plackett


1 Answers

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.

like image 166
Jerry Chou Avatar answered Oct 18 '25 06:10

Jerry Chou