gpt4 book ai didi

SwiftUI:UIImage(QRCode)在调用函数后不加载

转载 作者:搜寻专家 更新时间:2023-11-01 05:31:37 28 4
gpt4 key购买 nike

我花了很多时间试图诊断这个问题,但我完全卡住了,也许我遗漏了一些非常明显的东西。

已经尝试过:

  • 将返回的图像保存到磁盘以查看它是否正在生成正确地(它是)
  • 添加彩色背景呈现正方形,因此正在加载某些内容
  • .renderingMode(.original)标记图像
  • 使用 @State var img = UIImage() 并重新分配使用 Image(uiImage:img).onAppear {
    self.img = self.generateQRCode(来自:self.qrCodeString)!
    }

如有任何帮助,我们将不胜感激!

import SwiftUI

struct ContentView: View {

let qrCodeString = "BEGIN:VCARD \n" +
"VERSION:2.1 \n" +
"FN:John Peter \n" +
"N:Peter;John \n" +
"TITLE:Admin \n" +
"TEL;CELL:+91 431 524 2345 \n" +
"TEL;WORK;VOICE:+91 436 542 8374 \n" +
"EMAIL;WORK;INTERNET:John@ommail.in \n" +
"URL:www.facebook.com \n" +
"URL: www.instagram.com \n" +
"ADR;WORK:;;423 ofce sales Center;Newark;DE;3243;USA \n" +
"ORG:xxx Private limited \n" +
"END:VCARD"

var body: some View {
Image(uiImage: generateQRCode(from: qrCodeString)!)
}

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)
}
}
return nil
}
}

最佳答案

这似乎可以解决问题:

func generateQRCode(from string: String) -> UIImage? {

let data = string.data(using: String.Encoding.ascii)
var uiImage: UIImage?
if let filter = CIFilter(name: "CIQRCodeGenerator",
parameters: ["inputMessage": data,
"inputCorrectionLevel": "L"]) {

if let outputImage = filter.outputImage,
let cgImage = CIContext().createCGImage(outputImage,
from: outputImage.extent) {
let size = CGSize(width: outputImage.extent.width * 3.0,
height: outputImage.extent.height * 3.0)
UIGraphicsBeginImageContext(size)
if let context = UIGraphicsGetCurrentContext() {
context.interpolationQuality = .none
context.draw(cgImage,
in: CGRect(origin: .zero,
size: size))
uiImage = UIGraphicsGetImageFromCurrentImageContext()
}
UIGraphicsEndImageContext()
}
}
return uiImage
}

关于SwiftUI:UIImage(QRCode)在调用函数后不加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58172122/

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