I'm trying to create a simple filter app that filters the displayed image upon a button press.
@IBAction func applyFilter(sender: AnyObject) {
// Create an image to filter
let inputImage = CIImage(image: photoImageView.image)
// Create a random color to pass to a filter
let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]
// Apply a filter to the image
let filteredImage = inputImage.imageByApplyingFilter("CIHueAdjust", withInputParameters: randomColor)
// Render the filtered image
let renderedImage = context.createCGImage(filteredImage, fromRect: filteredImage.extent())
// Reflect the change back in the interface
photoImageView.image = UIImage(CGImage: renderedImage)
}
I keep getting the error:
Value of optional type 'UIImage?' not unwrapped.
What am I doing wrong here? I'm a Swift beginner.
An UIImageView does not always have an UIImage in its image property.
you should check before :
guard let img = photoImageView.image else { return }
let inputImage = CIImage(image: img)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With