Generating QR Codes with Core Image
I recently faced the problem of generating QR Codes on iOS. After spending some time on research I figured out that starting with iOS 7.0 you can generate QR Codes with Core Image Filters. The nice part about using Core Image Filters: No need for external libraries.
The Core Image Filter Reference is straight forward. You create a CIFilter object by passing two parameters:
- the name of the Filter (in this case CIQRCodeGenerator)
- a dictionary of input parameters (in this case the data that should be represented as QR Code and correction level)
Then you just need to access the
outputImage property on the CIFilter and pass that CIImage to the corresponding init method on UIImage. That’s it!
With the help of the inputCorrectionLevel parameter you can control the error resilience of the generated QR Code. A higher correction level results in a larger QR Code that contains additional data to allow for error correction. The CIQRCodeGenerator filter knows four correction modes:
|Correction Level||Error Resilience|
Scaling the QR Code
Depending on your needs, e.g., displaying it fullscreen on an iPad, the resulting UIImage might be too small. If you just tell the UIImageView to scale the content to fill the entire view you will see a pixelated QR Code. Fortunately, Apple allows you to apply transformations to the CIImage before you materialize it to an UIImage.
And there you have it: A huge crisp QR Code by only using Core Image Filters!