CIFilter output image nil
Asked Answered
C

2

8

I am using core image and I am applying a CIFilter sepia tone to my image. I run a 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 app crashes and says the output image is nil. 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.
        }



}
Clavius answered 7/2, 2015 at 19:23 Comment(0)
B
12

You cannot call UIImage(CIImage:) and use that UIImage as the image of a UIImageView. UIImageView requires a UIImage backed by a bitmap (CGImage). A UIImage instantiated with CIImage has no bitmap; it has no actual image, it's just a set of instructions for applying a filter. That is why your UIImageView's image is nil.

Blockbusting answered 7/2, 2015 at 19:59 Comment(5)
And see my book on how to extract a real image from a CIImage: apeth.com/iOSBook/ch15.html#_cifilter_and_ciimageBlockbusting
And for Swift code: github.com/mattneub/Programming-iOS-Book-Examples/blob/master/…Blockbusting
I find that the solution outlined by stackoverflow.com/users/1955870/fz does work (using CIContext). Is there an associated risk?Nicky
@Nicky That answer is what I am saying to do. That is totally different from calling UIImage(CIImage:) and sticking that in an image view; that is what I am saying not to do. - And see my book: apeth.com/iOSBook/ch15.html#_cifilter_and_ciimageBlockbusting
@Nicky linked to a user. More usefully, here is the link to the answer he refers to. This shows the correct way to code this.Jeanniejeannine
S
9

A couple of things here:

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

2) When creating the image back I'd suggest you to 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)
    }
}
Sands answered 7/2, 2015 at 19:59 Comment(1)
Great! Made is an IBInspectable UIImageView and posted the code on gist.github.com/SwiftArchitect/676007e8e1c77e632520Nicky

© 2022 - 2024 — McMap. All rights reserved.