简体   繁体   English

CIFilter输出图像为零

[英]CIFilter output image nil

I am using core image and I am applying a CIFilter sepia tone to my image. 我正在使用核心图像,我正在为我的图像应用CIFilter棕褐色调。 I run a filter once in viewDidLoad and then immediately call another function that adds the filter again. 我在viewDidLoad中运行一次过滤器,然后立即调用另一个再次添加过滤器的函数。 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.
        }



}

You cannot call UIImage(CIImage:) and use that UIImage as the image of a UIImageView. 你不能调用UIImage(CIImage:)并使用UIImage作为UIImageView的图像。 UIImageView requires a UIImage backed by a bitmap (CGImage). UIImageView需要一个由位图(CGImage)支持的UIImage。 A UIImage instantiated with CIImage has no bitmap; 用CIImage实例化的UIImage没有位图; 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. 这就是你的UIImageView图像为零的原因。

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. 1)使用CIImage构造函数基于非CIImage支持的UIImage创建CIImage是危险的,并且将返回nil或空CIImage。

2) When creating the image back I'd suggest you to use CIContext instead of UIImage(CIImage:). 2)当创建图像时,我建议你使用CIContext而不是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)
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM