Skip to content

Instantly share code, notes, and snippets.

@nh7a
Created August 1, 2015 21:27
Show Gist options
  • Save nh7a/4c20b13dc5bd5d44b380 to your computer and use it in GitHub Desktop.
Save nh7a/4c20b13dc5bd5d44b380 to your computer and use it in GitHub Desktop.
// let qrcodeImage = UIImage(message: "Hello, QRCode", size: 200)
extension UIImage {
// correctionLevel can be L(7%), M(15%), Q(25%), or H(30%)
convenience init?(message: String, size: CGFloat, correctionLevel: String = "M") {
let filter = CIFilter(name: "CIQRCodeGenerator")
filter.setValue(correctionLevel, forKey: "inputCorrectionLevel")
filter.setValue(message.dataUsingEncoding(NSUTF8StringEncoding)!, forKey: "inputMessage")
var uiImage = UIImage(CIImage: filter.outputImage, scale: 1.0, orientation: UIImageOrientation.Up)!
UIGraphicsBeginImageContext(CGSizeMake(size, size))
var ctx = UIGraphicsGetCurrentContext()
CGContextSetInterpolationQuality(ctx, kCGInterpolationNone)
uiImage.drawInRect(CGRectMake(0, 0, size, size))
uiImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
self.init(CGImage: uiImage.CGImage)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment