“This class is not key value coding-compliant” using CoreImage

醉酒当歌 提交于 2019-12-08 15:53:47

问题


I'm working on a simple MacOS command-line application in Swift. I created a custom CoreImage filter and having troubles to use it. The code compiles just fine but when it runs it exits with the following error:

*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key inputImage.'

Any help with this issue would be greatly appreciated. Tried searching for an answer on SO but since my application does not use Storyboards or Outlets (it's pure command-line) unfortunately I couldn't help myself.

My program breaks on this line:

filter.setValue(inputImage, forKey: kCIInputImageKey)

Here's the code I'm using:

class CustomFilter:CIFilter {
    var inputImage:CIImage?

    let kernelString = CIKernel(string:
        "kernel vec4 chromaKey( __sample s) { \n" +
            "  vec4 newPixel = s.rgba;" +
            "  newPixel[0] = 0.0;" +
            "  newPixel[2] = newPixel[2] / 2.0;" +
            "  return newPixel;\n" +
        "}"
    )

    override var outputImage:CIImage! {
        guard
            let inputImage = inputImage
        else {
            return nil
        }

        let extent = inputImage.extent

        let blur = kernelString?.apply(
            withExtent: extent,
            roiCallback: {
                (index, rect) in
                return rect
            },
            arguments: [inputImage])

        return blur!.cropping(to: extent)
    }
}

let filter = CustomFilter()

filter.setValue(inputImage, forKey: kCIInputImageKey) // it breaks here

guard
    let result = filter.outputImage
else {
    return nil
}

return result

回答1:


Key value coding-compliant properties must be marked as dynamic

dynamic var inputImage : CIImage?

and in Swift 4 even as @objc

@objc dynamic var inputImage : CIImage?


来源:https://stackoverflow.com/questions/46566076/this-class-is-not-key-value-coding-compliant-using-coreimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!