Getting error [UIImage extent]: unrecognized selector sent to instance
Asked Answered
A

2

5

I'm trying to apply a radial blur to my UIImageView but when I try this I get the error

[UIImage extent]: unrecognized selector sent to instance

The code I'm using is from the example on: https://developer.apple.com/documentation/coreimage/selectively_focusing_on_an_image

let h = bgImage.image!.size.height
let w = bgImage.image!.size.width
guard let radialMask = CIFilter(name:"CIRadialGradient") else {
    return
}
let imageCenter = CIVector(x:0.55 * w, y:0.6 * h)
radialMask.setValue(imageCenter, forKey:kCIInputCenterKey)
radialMask.setValue(0.2 * h, forKey:"inputRadius0")
radialMask.setValue(0.3 * h, forKey:"inputRadius1")
radialMask.setValue(CIColor(red:0, green:1, blue:0, alpha:0),
                    forKey:"inputColor0")
radialMask.setValue(CIColor(red:0, green:1, blue:0, alpha:1),
                    forKey:"inputColor1")

guard let maskedVariableBlur = CIFilter(name:"CIMaskedVariableBlur") else {
    return
}
maskedVariableBlur.setValue(bgImage.image, forKey: kCIInputImageKey)
maskedVariableBlur.setValue(10, forKey: kCIInputRadiusKey)
maskedVariableBlur.setValue(radialMask.outputImage, forKey: "inputMask")
let selectivelyFocusedCIImage = maskedVariableBlur.outputImage/

In which bgImage is a UIImageView

What am I doing wrong here?

Argumentative answered 9/11, 2018 at 14:12 Comment(1)
Make sure you are setting an image to the image view before applying the filter, safely unwrap optional values as sh_khan does and remove the / on the last line.Smarm
M
5

You need

guard let image = maskedVariableBlur?.image, cgimg = image.CGImage else {
    print("imageView doesn't have an image!")
    return
}

as

let coreImage = CIImage(CGImage:cgimg)
maskedVariableBlur.setValue(coreImage, forKey: kCIInputImageKey)

expects a CIImage not a UIImage

Minutiae answered 9/11, 2018 at 14:20 Comment(1)
Thanks this worked! But somehow my image got smaller than the whole width after applying the blur. Any idea on how that can have happened?Argumentative
G
1

I see two issues - One is your explicitly unwrapping the optional. let h = bgImage.image!.size.height let w = bgImage.image!.size.width Please use guard here to avoid unexpected crashes

  • Second issue is bgImage.image!.size.height. Here you should be using bgImage.image.CIImage.size or something like @image.CIImage.size.

Please refer below similar post. I hope this should help

Unrecognized selector sent to UIImage?

Goatish answered 9/11, 2018 at 14:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.