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.
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…