【问题标题】:Getting error [UIImage extent]: unrecognized selector sent to instance出现错误 [UIImage 范围]:无法识别的选择器发送到实例
【发布时间】:2019-04-13 02:51:18
【问题描述】:

我正在尝试对我的 UIImageView 应用径向模糊,但是当我尝试此操作时出现错误

[UIImage 范围]:发送到实例的无法识别的选择器

我使用的代码来自以下示例: 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/

其中bgImageUIImageView

我在这里做错了什么?

【问题讨论】:

  • 确保在应用过滤器之前将图像设置为图像视图,像 sh_khan 一样安全地展开可选值并删除最后一行的 /。

标签: ios swift xcode core-image


【解决方案1】:

你需要

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

作为

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

需要 CIImage 而不是 UIImage

【讨论】:

  • 感谢这工作!但不知何故,我的图像在应用模糊后变得小于整个宽度。知道这是怎么发生的吗?
【解决方案2】:

我看到两个问题 - 一个是你明确解开可选的。 让 h = bgImage.image!.size.height 让 w = bgImage.image!.size.width 请在此处使用防护以避免意外崩溃

  • 第二个问题是 bgImage.image!.size.height。在这里你应该使用 bgImage.image.CIImage.size 或类似@image.CIImage.size 的东西。

请参考下面类似的帖子。我希望这会有所帮助

Unrecognized selector sent to UIImage?

【讨论】:

    猜你喜欢
    • 2011-12-17
    • 2014-03-24
    • 2012-04-04
    • 1970-01-01
    • 1970-01-01
    • 2017-04-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多