gpt4 book ai didi

ios - 将 UIImage 转换为 (NS)Data 会产生 nil?

转载 作者:行者123 更新时间:2023-11-28 05:57:57 25 4
gpt4 key购买 nike

<分区>

我拼命地尝试在 Swift 中创建一个二维码并将图像转换为 (NS)Data-String。它应该在稍后充当 HTML 文件中的图像。尽管二维码创建完美,但转换为数据字符串仍然会生成 nil。有谁知道我的代码有什么问题吗?

let dataString = "some text or code or whatever"
let data = dataString.data(using: .utf8)

if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
let transform = CGAffineTransform(scaleX: 3, y: 3)

if let output = filter.outputImage?.transformed(by: transform) {

let bild = UIImage(ciImage: output) // <-- works quite well, image is shown in ImageView
let bildData = UIImageJPEGRepresentation(bild, 1.0) // <-- produces NIL

let bildString = bildData?.base64EncodedString(options: .lineLength64Characters)
//also tried: let bildString: String = String(data: bildData, encoding: .utf8)!
print("QRCODE-String: \(bildString)") // NIL
}
}

我也试过 UIImagePNGRepresentation() 得到了同样的结果。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com