小编典典

CIFilter输出图像无

swift

我正在使用核心图像,并且将CIFilter棕褐色调应用于图像。我在viewDidLoad中运行了一次过滤器,然后立即调用另一个再次添加过滤器的函数。由于某些原因,当我尝试访问输出图像时,应用程序崩溃并说输出图像为nil。有人知道为什么会这样吗?

谢谢

    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.
        }



}

阅读 193

收藏
2020-07-07

共1个答案

小编典典

您不能调用UIImage(CIImage:)该UIImage并将其用作UIImageView的图像。UIImageView需要一个由位图(CGImage)支持的UIImage。用CIImage实例化的UIImage没有位图;它没有实际图像,只是一组应用滤镜的说明。这就是为什么UIImageView的图像为nil的原因。

2020-07-07