Swift QR Code Generator (iOS/macOS): EFQRCode Library
QR codes have become an integral part of modern applications — from authentication to sharing links and contacts. If you are developing for iOS or macOS in Swift, you don't need to write complex encoding algorithms from scratch. The ready-made EFQRCode library provides a convenient API for creating, reading, and customizing QR codes. In this article, we will explore its capabilities, installation, and usage examples.
What is EFQRCode?
EFQRCode is an open-source library written in Swift. It supports iOS (starting from version 9.0) and macOS (10.11+). Key features:
- Generate QR codes from strings, URLs, vCard contacts, and text.
- Customize color, background, size, and error correction level.
- Add watermarks (logos, images) to the center of the QR code.
- Create animated QR codes (GIF).
- Decode QR codes from images.
The library uses the built-in CIFilter from Core Image but adds many convenient wrappers and customizations.
Installation via Swift Package Manager
Add the EFQRCode package to your Xcode project:
// In Xcode: File → Add Packages...// Paste the repository URL:https://github.com/EFPrefix/EFQRCode.git// Select the version (recommended "Up to Next Major")Or manually add the dependency in Package.swift:
dependencies: [ .package(url: "https://github.com/EFPrefix/EFQRCode.git", from: "6.0.0")]Basic QR Code Generation Example
The simplest way to create a QR code from a string:
import EFQRCode
// Create a QR code from textif let image = EFQRCode.generate( content: "https://pythonlib.ru", size: EFIntSize(width: 300, height: 300)) { // Use the image (CGImage) for display // For example, in UIImageView on iOS: let uiImage = UIImage(cgImage: image) imageView.image = uiImage}The result is a standard black-and-white QR code sized 300x300 pixels.
Customizing Color and Background
You can set custom foreground and background colors:
import EFQRCode
let qrImage = EFQRCode.generate( content: "Hello, Swift!", size: EFIntSize(width: 400, height: 400), backgroundColor: UIColor.yellow.cgColor, foregroundColor: UIColor.blue.cgColor, inputCorrectionLevel: .h // maximum error correction)The inputCorrectionLevel parameter accepts values .l (7%), .m (15%), .q (25%), .h (30%). The higher the level, the more data can be recovered if damaged.
Adding a Logo (Watermark)
To place a logo in the center of the QR code, use the generateWithWatermark method:
import EFQRCode
let watermarkImage = UIImage(named: "logo")?.cgImagelet qrWithLogo = EFQRCode.generateWithWatermark( content: "https://pythonlib.ru", size: EFIntSize(width: 500, height: 500), watermark: watermarkImage, watermarkMode: .center)Important: the logo should not occupy more than 30% of the area, otherwise the code may not be readable.
Creating an Animated QR Code (GIF)
EFQRCode supports generating animated QR codes. For example, you can create a GIF with changing colors:
import EFQRCode
let qrCodeGIF = EFQRCode.generateGIF( content: "Animated QR", size: EFIntSize(width: 200, height: 200), data: [someData] // array of frames)For simple animation, you can use predefined colors. See the library documentation for details.
Decoding a QR Code from an Image
To read a QR code from a photo or screenshot:
import