Last active
October 31, 2024 18:41
-
-
Save daoseng33/e72cd350d9903b74595ae34114a67f6b to your computer and use it in GitHub Desktop.
Generate high resolution QR Code in swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static func generateQRCode(from string: String, imageView: UIImageView) -> UIImage? { | |
let data = string.data(using: String.Encoding.ascii) | |
if let filter = CIFilter(name: "CIQRCodeGenerator") { | |
filter.setValue(data, forKey: "inputMessage") | |
// L: 7%, M: 15%, Q: 25%, H: 30% | |
filter.setValue("M", forKey: "inputCorrectionLevel") | |
if let qrImage = filter.outputImage { | |
let scaleX = imageView.frame.size.width / qrImage.extent.size.width | |
let scaleY = imageView.frame.size.height / qrImage.extent.size.height | |
let transform = CGAffineTransform(scaleX: scaleX, y: scaleY) | |
let output = qrImage.transformed(by: transform) | |
return UIImage(ciImage: output) | |
} | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment