HovyTech HovyTech - 7 months ago 108
Swift Question

Swift Image Filter

The filtering on the photo works but I'm stuck with this problem.




When I run this code:



let originalImage = CIImage(image: imageView.image)

var filter = CIFilter(name: "CIPhotoEffectMono")

filter.setDefaults()
filter.setValue(originalImage, forKey: kCIInputImageKey)

var newImage = UIImage(CIImage: filter.outputImage)
imageView.image = newImage





The first error is:



BSXPCMessage received error for message: Connection interrupted





The second error is:



fatal error: unexpectedly found nil while unwrapping an Optional value





Then the app crashes.

How would I fix it and the errors?

Answer

I Found A Solution:

Updated

let originalImage = CIImage(image: imageView.image)
var filter = CIFilter(name: "CIPhotoEffectMono")
filter.setValue(originalImage, forKey: kCIInputImageKey)
let context = CIContext(options: [kCIContextUseSoftwareRenderer: true])
let outputImage = context.createCGImage(filter.outputImage, fromRect: filter.outputImage.extent())
var newImage = UIImage(CGImage: outputImage)
imageView.image = newImage

*Based on Lamar's code.


CIContext(options: nil) was causing BSXPCMessage received error for message: Connection interrupted error.

SO replace:

CIContext(options: nil) with CIContext(options: [kCIContextUseSoftwareRenderer: true])


Thanks For The Help:

Lamar

The app doesn't crash anymore.