使用 CoreImage 生成二维码图像。
import SwiftUI
import CoreImage.CIFilterBuiltins
struct QRView: View {
let qrCode: String
@State private var image: UIImage?
var body: some View {
ZStack {
if let image = image {
Image(uiImage: image)
.resizable()
.interpolation(.none)
.frame(width: 210, height: 210)
}
}
.onAppear {
generateImage()
}
}
private func generateImage() {
guard image == nil else { return }
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
filter.message = Data(qrCode.utf8)
guard
let outputImage = filter.outputImage,
let cgImage = context.createCGImage(outputImage, from: outputImage.extent)
else { return }
self.image = UIImage(cgImage: cgimg)
}
}
Swift社区是由 Swift 爱好者共同维护的公益组织,我们在国内以微信公众号的运营为主,我们会分享以 Swift实战、SwiftUl、Swift基础为核心的技术内容,也整理收集优秀的学习资料。
特别感谢 Swift社区 编辑部的每一位编辑,感谢大家的辛苦付出,为 Swift社区 提供优质内容,为 Swift 语言的发展贡献自己的力量,排名不分先后:张安宇@微软[1]、戴铭@快手[2]、展菲@ESP[3]、倪瑶@Trip.com[4]、杜鑫瑶@新浪[5]、韦弦@Gwell[6]、张浩@讯飞[7]、张星宇@ByteDance[8]、郭英东@便利蜂[9]
[1]张安宇: https://blog.csdn.net/mobanchengshuang
[2]戴铭: https://ming1016.github.io
[3]展菲: https://github.com/fanbaoying
[4]倪瑶: https://github.com/niyaoyao
[5]杜鑫瑶: https://weibo.com/u/3878455011
[6]韦弦: https://www.jianshu.com/u/855d6ea2b3d1
[7]张浩: https://github.com/zhanghao19920218
[8]张星宇: https://github.com/bestswifter
[9]郭英东: https://github.com/EmingK