You can use the QR code as a mask and blend it with a solid color to effectively colorize it. So you can, for example, create a black and a white version and use the register(...) method of UIImage to "bundle" them both into one dynamic image:
let qrCode = filter.outputImage!.transformed(by: transform)
// Use the QR code as a mask for blending with a color.
// Note that we need to invert the code for that, so the actual code becomes white
// and the background becomes black, because white = let color through, black = transparent.
let maskFilter = CIFilter.blendWithMask()
maskFilter.maskImage = qrCode.applyingFilter("CIColorInvert")
// create a version of the code with black foreground...
maskFilter.inputImage = CIImage(color: .black)
let blackCIImage = maskFilter.outputImage!
// ... and one with white foreground
maskFilter.inputImage = CIImage(color: .white)
let whiteCIImage = maskFilter.outputImage!
// render both images
let blackImage = context.createCGImage(blackCIImage, from: blackCIImage.extent).map(UIImage.init)!
let whiteImage = context.createCGImage(whiteCIImage, from: whiteCIImage.extent).map(UIImage.init)!
// use black version for light mode
qrImage = blackImage
// assign the white version to be used in dark mode
qrImage.imageAsset?.register(whiteImage, with: UITraitCollection(userInterfaceStyle: .dark))
return qrImage
Topic:
Media Technologies
SubTopic:
General
Tags: