imageView.image = UIImage(ciImage: ) 不会更新第二次
imageView.image = UIImage(ciImage: ) won't update a second time
案例:我在视图中有一个 UISwitch 按钮。当开关关闭时,imageView 将显示从字符串生成的 QRCode 图像。打开时,它将显示从字符串生成的条形码图像。我从 https://www.hackingwithswift.com/ 复制了 QRCode 和条形码生成函数,它们工作正常。
问题是 imageView 仅在第一次显示生成的图像。在 QRCode 和条形码之间切换时,imageView 将不再更新。我 100% 确定 UISwitch 正在工作,函数被调用并返回生成的图像。我用断点和打印测试了很多次,函数没有返回 nil 或空图像。
这里是二维码生成码
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()
但运气不好。
快速搜索...似乎是“错误”或更改...
将您的生成器代码更改为此似乎可以解决问题:
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
let context = CIContext()
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) {
if let retImg = context.createCGImage(output, from: output.extent) {
return UIImage(cgImage: retImg)
}
}
}
return nil
}
func generateBarcode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
let context = CIContext()
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) {
if let retImg = context.createCGImage(output, from: output.extent) {
return UIImage(cgImage: retImg)
}
}
}
return nil
}
案例:我在视图中有一个 UISwitch 按钮。当开关关闭时,imageView 将显示从字符串生成的 QRCode 图像。打开时,它将显示从字符串生成的条形码图像。我从 https://www.hackingwithswift.com/ 复制了 QRCode 和条形码生成函数,它们工作正常。
问题是 imageView 仅在第一次显示生成的图像。在 QRCode 和条形码之间切换时,imageView 将不再更新。我 100% 确定 UISwitch 正在工作,函数被调用并返回生成的图像。我用断点和打印测试了很多次,函数没有返回 nil 或空图像。
这里是二维码生成码
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()
但运气不好。
快速搜索...似乎是“错误”或更改...
将您的生成器代码更改为此似乎可以解决问题:
func generateQRCode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
let context = CIContext()
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) {
if let retImg = context.createCGImage(output, from: output.extent) {
return UIImage(cgImage: retImg)
}
}
}
return nil
}
func generateBarcode(from string: String) -> UIImage? {
let data = string.data(using: String.Encoding.ascii)
let context = CIContext()
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) {
if let retImg = context.createCGImage(output, from: output.extent) {
return UIImage(cgImage: retImg)
}
}
}
return nil
}