CIFilter输出图像为零 [英] CIFilter output image nil
问题描述
我正在使用核心图像,我正在为我的图像应用CIFilter棕褐色调。我在viewDidLoad中运行一次过滤器,然后立即调用另一个再次添加过滤器的函数。出于某种原因,当我尝试访问输出图像时,应用程序崩溃并说输出图像为零。有人知道为什么会这样吗?
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?
谢谢
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.
}
}
推荐答案
你不能调用 UIImage(CIImage:)
并使用该UIImage作为UIImageView的图像。 UIImageView需要一个由位图(CGImage)支持的UIImage。用CIImage实例化的UIImage没有位图;它没有实际图像,它只是一组应用过滤器的指令。这就是你的UIImageView图像为零的原因。
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.
这篇关于CIFilter输出图像为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!