here\'s my code for generating QRCode image
+ (UIImage *)generateQRCodeWithString:(NSString *)string {
NSData *stringData = [string dataUsingEncoding:NSU
Rewrite @Benoît Caron's answer in Swift 3.1:
func convertTextToQRCode(text: String, withSize size: CGSize) -> UIImage {
let data = text.data(using: String.Encoding.isoLatin1, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")!
filter.setValue(data, forKey: "inputMessage")
filter.setValue("L", forKey: "inputCorrectionLevel")
let qrcodeCIImage = filter.outputImage!
let cgImage = CIContext(options:nil).createCGImage(qrcodeCIImage, from: qrcodeCIImage.extent)
UIGraphicsBeginImageContext(CGSize(width: size.width * UIScreen.main.scale, height:size.height * UIScreen.main.scale))
let context = UIGraphicsGetCurrentContext()
context!.interpolationQuality = .none
context?.draw(cgImage!, in: CGRect(x: 0.0,y: 0.0,width: context!.boundingBoxOfClipPath.width,height: context!.boundingBoxOfClipPath.height))
let preImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
let qrCodeImage = UIImage(cgImage: (preImage?.cgImage!)!, scale: 1.0/UIScreen.main.scale, orientation: .downMirrored)
return qrCodeImage
}