Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value of optional type 'UIImage?' not unwrapped

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.

like image 599
Adriano Gravina Avatar asked May 19 '16 14:05

Adriano Gravina


1 Answers

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)
like image 139
CZ54 Avatar answered Oct 12 '22 04:10

CZ54