TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: QR Code Generator (CIFilter) colours for light/dark mode ?

Forums > SwiftUI

Using the CIFilter.qrCodeGenerator() to create a QR code I wanted to change the colours dynamically to suit Light/Dark mode, but was unable to figure out how to achieve this, is it possible please ?

struct QrCodeImage {

    let context = CIContext()

    func generateQRCode(from text: String) -> UIImage {

        var qrImage = UIImage(systemName: "xmark.circle") ?? UIImage()

        let data = Data(text.utf8)

        let filter = CIFilter.qrCodeGenerator()

        filter.setValue(data, forKey: "inputMessage")

        let transform = CGAffineTransform(scaleX: 2, y: 2)

        if let outputImage = filter.outputImage?.transformed(by: transform) {

            if let image = context.createCGImage(

                outputImage,

                from: outputImage.extent) {

                qrImage = UIImage(cgImage: image)

            }

        }

        return qrImage

    }
}

Further, I cannot see an option for the different modes and assume that any colour could be used, which would be a lot better for me.

ref: https://developer.apple.com/documentation/coreimage/ciqrcodegenerator

1      

The answer was to use the QRcode as a mask and to switch them with the Core Image filter "CIColorInvert" when the mode changes:

struct QrCodeImage {

    let context = CIContext()

    func generateQRCode(from text: String) -> UIImage {
        var qrImage = UIImage(systemName: "xmark.circle") ?? UIImage()
        let data    = Data(text.utf8)
        let filter  = CIFilter.qrCodeGenerator()

        // ref : https://stackoverflow.com/questions/57704885/how-can-i-check-ios-devices-current-userinterfacestyle-programmatically
        var osTheme: UIUserInterfaceStyle { return UIScreen.main.traitCollection.userInterfaceStyle }
        filter.setValue(data, forKey: "inputMessage")

        let transform = CGAffineTransform(scaleX: 2, y: 2)
        if let outputImage = filter.outputImage?.transformed(by: transform) {
            if let image = context.createCGImage(
                outputImage,
                from: outputImage.extent) {

                let maskFilter = CIFilter.blendWithMask()
                maskFilter.maskImage = outputImage.applyingFilter("CIColorInvert")

                maskFilter.inputImage = CIImage(color: .white)

                let darkCIImage = maskFilter.outputImage!
                maskFilter.inputImage = CIImage(color: .black)

                let lightCIImage = maskFilter.outputImage!

                let darkImage   = context.createCGImage(darkCIImage, from: darkCIImage.extent).map(UIImage.init)!
                let lightImage  = context.createCGImage(lightCIImage, from: lightCIImage.extent).map(UIImage.init)!

                qrImage = osTheme == .light ? lightImage : darkImage
            }
        }
        return qrImage
    }
}

h/t : https://developer.apple.com/forums/thread/701448?login=true&page=1#706613022

1      

Excellent find! Please mark your own answer as "Correct" !

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.