imageView.image = UIImage(ciImage: ) 不会再次更新
案例:我可以看到一个 UISwitch 按钮。当开关关闭时,imageView 将显示由字符串生成的 QRCode 图像。启用后,它将显示从字符串生成的条形码图像。我从 https://www.hackingwithswift.com/ 和 复制了 QRCode 和条形码生成函数他们工作得很好。
问题是 imageView 仅在第一次显示生成的图像。当在 QRCode 和条形码之间切换时,imageView 将不再更新。我 100% 确定 UISwitch 正在工作,调用函数并返回生成的图像。我已经用断点和打印进行了多次测试,这些函数不返回零或空图像。
这里是QRCode生成代码
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output) //<== it always returns this
}
}
return nil
}
这里是条形码生成代码
func generateBarcode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output) //<== it always returns this
}
}
return nil
}
这是我如何在QRCode和条形码之间切换并且逻辑是正确的
func toggleBarcode(isBarcode : Bool) {
if isBarcode {
qrImageView.image = generateBarcode(from: "Hacking with Swift")
} else {
qrImageView.image = generateQRCode(from: "Hacking with Swift")
}
}
当使用类似的东西进行测试时
qrImageView.image = UIImage(named: "myimage")
它每次都有效。
imageView 不会仅使用 UIImage(ciImage: ) 进行更新
我什至使用几个不同版本的 QR 和条形码生成代码进行了测试,它们都不会第二次更新 imageView。
我尝试过
qrImageView.setNeedsDisplay()
但没有运气。
Case: I have a UISwitch button in view. When switch is off, the imageView will display a QRCode image generated from a string. When on, it will display a barcode image generated from string. I copied the QRCode and barcode generation functions from https://www.hackingwithswift.com/ and they are working fine.
The problem is the imageView displays the generated image only for the first time. When switching between QRCode and barcode, the imageView won't update anymore. I am 100% sure the UISwitch is working, the functions are called and returning the generated image. I have tested multiple times with break points and printing, the functions are not returning nil or empty images.
Here is QRCode generation code
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output) //<== it always returns this
}
}
return nil
}
Here is barcode generation code
func generateBarcode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CICode128BarcodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.transformed(by: transform) {
return UIImage(ciImage: output) //<== it always returns this
}
}
return nil
}
Here is how I switch between QRCode and barcode and the logic is correct
func toggleBarcode(isBarcode : Bool) {
if isBarcode {
qrImageView.image = generateBarcode(from: "Hacking with Swift")
} else {
qrImageView.image = generateQRCode(from: "Hacking with Swift")
}
}
When testing with something like
qrImageView.image = UIImage(named: "myimage")
it works everytime.
The imageView won't just update with UIImage(ciImage: )
I even tested with a few different versions of QR and barcode generation codes they all don't update the imageView for a second time.
I tried
qrImageView.setNeedsDisplay()
but no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
快速搜索...似乎是一个“错误”或一个更改...
将生成器代码更改为此似乎可以纠正问题:
Quick searching ... seems to be either a "bug" or a change...
Changing your generator code to this seems to correct the issue: