imageView.image = UIImage(ciImage: ) 不会再次更新

发布于 2025-01-09 16:36:39 字数 2092 浏览 2 评论 0原文

案例:我可以看到一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2025-01-16 16:36:39

快速搜索...似乎是一个“错误”或一个更改...

将生成器代码更改为此似乎可以纠正问题:

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
}

Quick searching ... seems to be either a "bug" or a change...

Changing your generator code to this seems to correct the issue:

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文