Swift - UIImage 过滤器触发异常

Swift - UIImage filter triggering exception

我遵循了帮助创建二维码的在线教程。我的 objective 是创建的 QR 码将作为文件存储在 Parse DB 中,并在本地作为 UIImage 存储在 userObject 中。当我 运行 下面的代码时,它触发了以下错误:

2015-12-06 12:43:45.994 Mawq[7246:279805] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key inputImage.'

我使用的创建二维码的代码是:

print("1")
let data = userObject.username!.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
print("2")
let filter = CIFilter(name: "CIQRCodeGenerator")
print("3")
filter!.setValue(data, forKey: "QRImage")
print("4")
let newQRImage = UIImage(CIImage: filter!.outputImage!);
print("5")
user["qrCode"] = newQRImage;
print("6")
userObject.userQRCode = newQRImage;
print("7")

打印语句只是为了知道哪里触发了错误。显然如控制台所示,它在第 3 行打印后出错。我遵循的教程在这里:http://www.appcoda.com/qr-code-generator-tutorial/.

我要存放图片的userObject如下:

public class UserClass {

    var name: String?
    var email: String?
    var password: String?
    var mobile: String?
    var username: String?
    var tempToken: Int?
    var userQRCode: UIImage?
}

这是我第一次使用 UIKit 来实现这个结果...所以感谢任何帮助。

谢谢

只需使用教程中使用的相同代码:

filter!.setValue(data, forKey: "inputMessage")

data保存的是你想要二维码图片保存的数据。例如用户名:

let data = userObject.username!.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)

这就是您要做的全部。 不要 更改密钥。

这里用来设置过滤器不同值的叫做键值编码,你可以为特定的键输入任意值。然后过滤器读取某些键的值并解释和评估它们。密钥集是固定的。过滤器需要 inputMessage 存在。但是,过滤器 期望传入 QRImage