Swift QR Code Generator: Core Image, CIFilter and Custom Solutions

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Generating QR Codes in Swift (iOS/macOS): From Core Image to Custom Solutions



QR codes have become an integral part of modern applications — from sharing links to authorization and payments. On Apple platforms (iOS, macOS, watchOS, tvOS) there is a built-in powerful tool for generating QR codes — the Core Image framework with the CIQRCodeGenerator filter. In this article, we will thoroughly examine how to create your own QR code generator in Swift, including color and size customization, adding a logo, and integration with SwiftUI.



Basics: Core Image and CIQRCodeGenerator



Core Image is Apple's built-in framework for image processing. The CIQRCodeGenerator filter allows you to create a QR code from any string (URL, text, contact data). It supports error correction (levels L, M, Q, H) and encoding up to 7089 characters (depending on the correction level).



A basic example of generating a QR code as a UIImage:



import UIKitimport CoreImage

func generateQRCode(from string: String) -> UIImage? { let data = string.data(using: .utf8) guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return nil } filter.setValue(data, forKey: "inputMessage") filter.setValue("H", forKey: "inputCorrectionLevel") // Maximum error correction guard let outputImage = filter.outputImage else { return nil } // Increase resolution (default is small) let transform = CGAffineTransform(scaleX: 10, y: 10) let scaledImage = outputImage.transformed(by: transform) return UIImage(ciImage: scaledImage)}

// Usageif let qrImage = generateQRCode(from: "https://pythonlib.ru") { // Display qrImage in UIImageView}


Color and Background Customization



By default, the QR code is black and white. To change colors, you need to convert CIImage to UIImage, then perform filling using Core Graphics or CIFilter. The easiest way is to use CIFalseColor:



func generateColoredQRCode(from string: String, color: UIColor, backgroundColor: UIColor) -> UIImage? {    guard let data = string.data(using: .utf8),          let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil }    qrFilter.setValue(data, forKey: "inputMessage")    qrFilter.setValue("M", forKey: "inputCorrectionLevel")        guard let qrImage = qrFilter.outputImage else { return nil }        // Apply color filter    let colorFilter = CIFilter(name: "CIFalseColor")    colorFilter?.setValue(qrImage, forKey: "inputImage")    colorFilter?.setValue(CIColor(color: color), forKey: "inputColor0")   // Module color    colorFilter?.setValue(CIColor(color: backgroundColor), forKey: "inputColor1") // Background color        guard let coloredImage = colorFilter?.outputImage else { return nil }        let transform = CGAffineTransform(scaleX: 10, y: 10)    let scaledImage = coloredImage.transformed(by: transform)        return UIImage(ciImage: scaledImage)}


Adding a Logo to the Center of the QR Code



For branding, it is often necessary to insert a logo in the center. This reduces readability, so it is better to use a high error correction level (H). The algorithm: first create the QR code, then overlay a UIImage with the logo on top of it.



func generateQRWithLogo(from string: String, logo: UIImage, qrColor: UIColor = .black, bgColor: UIColor = .white) -> UIImage? {    guard let qrImage = generateColoredQRCode(from: string, color: qrColor, backgroundColor: bgColor) else { return nil }        let qrSize = qrImage.size    let logoSize = CGSize(width: qrSize.width * 0.25, height: qrSize.height * 0.25) // 25% of QR    let logoOrigin = CGPoint(x: (qrSize.width - logoSize.width) / 2,                             y: (qrSize.height - logoSize.height) / 2)        UIGraphicsBeginImageContextWithOptions(qrSize, fa                    

Recommendations