CIFilter Output Image

I use the main image and I apply the Sipilter sepia signal to my image. I run the filter once in viewDidLoad, and then immediately call another function that adds the filter again. For some reason, when I try to access the output image, the application crashes and says that the output image is zero. Does anyone know why this is happening?

thanks

import UIKit class ViewController: UIViewController { @IBOutlet weak var myimage: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. let image = CIImage(image: myimage.image) let filter = CIFilter(name: "CISepiaTone") filter.setDefaults() filter.setValue(image, forKey: kCIInputImageKey) myimage.image = UIImage(CIImage: filter.outputImage) self.reapplyFilter() } func reapplyFilter(){ let image = CIImage(image: myimage.image) let filter = CIFilter(name: "CISepiaTone") filter.setDefaults() filter.setVa lue(image, forKey: kCIInputImageKey) //ERROR HERE: fatal error: unexpectedly found nil while unwrapping an Optional value myimage.image = UIImage(CIImage: filter.outputImage) //ERROR } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } 
+7
ios swift core-image cifilter
source share
2 answers

You cannot call UIImage(CIImage:) and use this UIImage as an UIImageView image. UIImageView requires a UIImage supported by a bitmap (CGImage). UIImage created using CIImage does not have a bitmap; it does not have an actual image, it is just a set of instructions for using the filter. This is why your UIImageView image is zero.

+10
source share

A few things here:

1) Using the CIImage constructor to create a CIImage based on an unsupported CIImage UIImage is dangerous and will return zero or an empty CIImage.

2) When creating the inverse image, I suggest you use CIContext instead of UIImage (CIImage :).

Example:

 class ViewController: UIViewController { @IBOutlet weak var myimage: UIImageView! override func viewDidLoad() { super.viewDidLoad() myimage.backgroundColor = UIColor.redColor() self.applyFilter() self.applyFilter() } func applyFilter(){ let image = CIImage(CGImage: myimage.image?.CGImage) let filter = CIFilter(name: "CISepiaTone") filter.setDefaults() filter.setValue(image, forKey: kCIInputImageKey) let context = CIContext(options: nil) let imageRef = context.createCGImage(filter.outputImage, fromRect: image.extent()) myimage.image = UIImage(CGImage: imageRef) } } 
+8
source share

All Articles