Code to generate QR image in Swift 2.0.
Add Apple's 'Core media' framework to project.
let reqStr = “string to convert as QR code”
let data = reqStr.dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter!.setValue(data, forKey: "inputMessage")
let qrImage:CIImage = filter!.outputImage!
//qrImageView is a IBOutlet of UIImageView
let scaleX = qrImageView.frame.size.width / qrImage.extent.size.width
let scaleY = qrImageView.frame.size.height / qrImage.extent.size.height
let resultQrImage = qrImage.imageByApplyingTransform(CGAffineTransformMakeScale(scaleX, scaleY))
qrImageView.image = UIImage(CIImage: resultQrImage)
No comments:
Post a Comment