Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

swift generate qr code with transparent background

Tags:

ios

swift

in my swift 2 app i can generate a QR Code like this:

let data = "1234567890".dataUsingEncoding(NSISOLatin1StringEncoding, allowLossyConversion: false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter!.setValue(data, forKey: "inputMessage")
filter!.setValue("Q", forKey: "inputCorrectionLevel")
qrcodeImage = filter!.outputImage
let transformedImage = qrcodeImage.imageByApplyingTransform(CGAffineTransformMakeScale(150, 150))
QRCodeImage.image = UIImage(CIImage: transformedImage)

but my QR Code get an white background image, but i would like to have an transparent background.

i tried something like this:

QRCodeImage.backgroundColor = UIColor.clearColor()

but this doesn't work. any idea ? :)

like image 744
Stack108 Avatar asked Oct 29 '25 11:10

Stack108


1 Answers

It seems like the CIQRCodeGenerator filter always uses black and white.

You can pass the output into a CIMaskToAlpha filter to convert it to transparent:

And first you might want to use CIColorInvert to swap white & black.

like image 166
jtbandes Avatar answered Oct 31 '25 01:10

jtbandes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!