I have a custom QR code generator that is pretty simple right now. But, I want to do two things.
- Change the color of the QR code
- Change the shape of the individual squares (e.g. round the corners)
If anyone is familiar with custom QR code generation, I'd be so happy if you could help. Here's the code for the generator function:
func generateQRCode() {
var urlString = Twitter.sharedInstance.firebaseDownloadLink?.absoluteString
if let rangeOfHTTPS = urlString?.range(of: "https://") {
urlString?.removeSubrange(rangeOfHTTPS)
}
guard var safeString = urlString else {
return
}
safeString = "https://www.twitter.com/card?"+safeString
log.info("encoded link: \(safeString)")
let data = safeString.data(using: String.Encoding.ascii)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
let transform = CGAffineTransform(scaleX: 3, y: 3)
if let output = filter.outputImage?.applying(transform) {
let qrCode = UIImage(ciImage: output)
//let rawImageRef: CGImage = qrCode.cgImage!
let colorMasking: [CGFloat] = [222, 255, 222, 255, 222, 255]
UIGraphicsBeginImageContext(qrCode.size)
var rawImageRef: CGImage = CIContext.init(options: nil).createCGImage(output, from: output.extent)!
let maskedImageRef = rawImageRef.copy(maskingColorComponents: colorMasking)
UIGraphicsGetCurrentContext()?.translateBy(x: 0.0, y: qrCode.size.height)
UIGraphicsGetCurrentContext()!.scaleBy(x: 1.0, y: -1.0)
UIGraphicsGetCurrentContext()?.draw(maskedImageRef!, in: CGRect(x: 0, y: 0, width: qrCode.size.width, height: qrCode.size.height))
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
qrCodeView = UIImageView(image: result)
self.backgroundImage.addSubview(qrCodeView!)
qrCodeView!.frame = CGRect(x: 96, y: 125, width: 180, height: 180)
}
}
}
Any help would be much appreciated. Thanks so much in advance!
Cheers, Theo