import SwiftUI
import CoreImage.CIFilterBuiltins
struct ContentView: View {
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
@State var name = "Tom"
@State var emailAddress = "Your@site.com"
var body: some View {
VStack {
TextField("Name", text:$name).textContentType(.name).font(.title).padding(.horizontal).background(Color.gray)
TextField("Name", text:$emailAddress).textContentType(.emailAddress).font(.title).padding([.horizontal, .bottom]).background(Color.gray)
Spacer()
Image(uiImage: generateQRCode(from: "\(name)\n\(emailAddress)")).interpolation(.none)
.resizable().scaledToFit().frame(width: 200, height: 200, alignment: .center)
}.navigationBarTitle("Your Code")
}
func generateQRCode(from string:String) -> UIImage {
let data = Data(string.utf8)
filter.setValue(data, forKey: "inputMessage")
if let outputImage = filter.outputImage {
if let cgimg = context.createCGImage(outputImage, from: outputImage.extent) {
return UIImage(cgImage: cgimg)
}
}
return UIImage(systemName: "xmark.circle") ?? UIImage()
}
}
SwiftUI:CIFilter qrCodeGenerator
最新推荐文章于 2024-01-27 21:40:54 发布