Is there a way to generate QR code image on iOS

Using Swift 2

import UIKit
import CoreImage

func createQRFromString(str: String) -> CIImage? {
    let stringData = str.dataUsingEncoding(NSUTF8StringEncoding)
    
    let filter = CIFilter(name: "CIQRCodeGenerator")
    
    filter?.setValue(stringData, forKey: "inputMessage")
    
    filter?.setValue("H", forKey: "inputCorrectionLevel")
    
    return filter?.outputImage
}

if let img = createQRFromString("Hello world program created by someone") {
    let somImage = UIImage(CIImage: img, scale: 1.0, orientation: UIImageOrientation.Down)
}

enter image description here

Swift 3.0

import UIKit
import CoreImage

func createQRFromString(_ str: String) -> CIImage? {
        let stringData = str.data(using: String.Encoding.utf8)
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        
        filter?.setValue(stringData, forKey: "inputMessage")
        
        filter?.setValue("H", forKey: "inputCorrectionLevel")
        
        if let img = createQRFromString("Hello world program created by someone") {
            let somImage = UIImage(ciImage: img, scale: 1.0, orientation: UIImageOrientation.down)
        }
        
        return filter?.outputImage
    }

if let img = createQRFromString("Hello world program created by someone") {
            let somImage = UIImage(ciImage: img, scale: 1.0, orientation: UIImageOrientation.down)
        }

Swift 4.2

private func createQRFromString(str: String) -> CIImage? {
        let stringData = str.data(using: .utf8)
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        filter?.setValue(stringData, forKey: "inputMessage")
        filter?.setValue("H", forKey: "inputCorrectionLevel")
        
        return filter?.outputImage
    }
    
    var qrCode: UIImage? {
        if let img = createQRFromString(str: "Hello world program created by someone") {
            let someImage = UIImage(
                ciImage: img,
                scale: 1.0,
                orientation: UIImage.Orientation.down
            )
            return someImage
        }
        
        return nil
    }

Xcode 12.4 or Swift version >= 5.2

import UIKit
import CoreImage

func createQRFromString(str: String) -> CIImage? {
  let stringData = str.data(using: .utf8)
  
  let filter = CIFilter(name: "CIQRCodeGenerator")
  
  filter?.setValue(stringData, forKey: "inputMessage")
  
  filter?.setValue("H", forKey: "inputCorrectionLevel")
  
  return filter?.outputImage
}

if let img = createQRFromString(str: "Hello world program created by someone") {
  let somImage = UIImage(ciImage: img, scale: 1.0, orientation: .down)
}

Since iOS 7, you can use a Core Image filter to generate QR images. See the final tip here:

- (CIImage *)createQRForString:(NSString *)qrString {
    NSData *stringData = [qrString dataUsingEncoding: NSISOLatin1StringEncoding];

    CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [qrFilter setValue:stringData forKey:@"inputMessage"];

    return qrFilter.outputImage;
}

For Obj-C version that perfectly works for me, I've mixed answers पवन and Teja Kumar Bethina:

NSString *qrString = @"My string to encode";
NSData *stringData = [qrString dataUsingEncoding: NSUTF8StringEncoding];

CIFilter *qrFilter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
[qrFilter setValue:stringData forKey:@"inputMessage"];
[qrFilter setValue:@"H" forKey:@"inputCorrectionLevel"];

CIImage *qrImage = qrFilter.outputImage;
float scaleX = self.qrImageView.frame.size.width / qrImage.extent.size.width;
float scaleY = self.qrImageView.frame.size.height / qrImage.extent.size.height;

qrImage = [qrImage imageByApplyingTransform:CGAffineTransformMakeScale(scaleX, scaleY)];

self.qrImageView.image = [UIImage imageWithCIImage:qrImage
                                             scale:[UIScreen mainScreen].scale
                                       orientation:UIImageOrientationUp];

Tags:

Ios

Qr Code