CoreImage 안에 QR 생성하는 CIFilter라는 것이 있음
import CoreImage.CIFilterBuiltins
extension UIImage {
static func generateQR(string: String, placeholder: UIImage = UIImage(systemName: "photo")!) -> UIImage {
let context = CIContext()
let filter = CIFilter.qrCodeGenerator()
filter.message = Data(string.utf8)
if let outputImage = filter.outputImage {
if let cgImage = context.createCGImage(outputImage, from: outputImage.extent) {
return UIImage(cgImage: cgImage)
}
}
return placeholder
}
}
Image(uiImage: UIImage.generateQR(string: "<https://google.com>"))
.interpolation(.none)
.resizable()
.scaledToFit()
.frame(width: 150, height: 150)
interpolation(.none)을 안하면 결과가 흐릿하게 나옴