0% found this document useful (0 votes)
32 views

How To Generate A QR Image in iOS App Using Swift - Stack Overflow

Uploaded by

mssrinivas.s99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

How To Generate A QR Image in iOS App Using Swift - Stack Overflow

Uploaded by

mssrinivas.s99
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

17/07/2024, 19:43 How to generate a QR image in iOS app using swift - Stack Overflow

How to generate a QR image in iOS app using swift [closed]


Asked 4 years, 10 months ago Modified 1 year, 7 months ago Viewed 6k times Part of Mobile Development Collective

Closed. This question needs to be more focused. It is not currently accepting answers.
4 Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question

Hi I am working on an iOS project i want to generate a customised QR code but have no idea how to do it. I am attaching a sample also.

i want to generate a QR Code Like this in Swift Language. Thanks


https://stackoverflow.com/questions/57703239/how-to-generate-a-qr-image-in-ios-app-using-swift 1/6
17/07/2024, 19:43 How to generate a QR image in iOS app using swift - Stack Overflow

ios swift qr-code

Share Improve this question Follow edited Mar 11, 2022 at 6:21 asked Aug 29, 2019 at 5:00
Hasnain ahmad
301 1 10 33

1 You could achieve the same using CIFilter, CGAffineTransform, code snippet in my answer below. Please mark as answered if it answers your query
– Srikanth Balaji Aug 29, 2019 at 5:19
may be you can look here: stackoverflow.com/q/28801350/5575752 – Ronak Chaniyara Aug 29, 2019 at 6:27

3 Answers Sorted by: Highest score (default)

Here is the code for generating the QR code with logo image in centre:
10 import UIKit
import EventKit
import CoreImage

class ViewController: UIViewController {

@IBOutlet weak var imageView: UIImageView!

override func viewDidLoad() {


super.viewDidLoad()

guard let qrURLImage = URL(string:


"https://www.thepramodkumar.com/")?.qrImage(using: #colorLiteral(red:
0.3490196078, green: 0.768627451, blue: 0.6823529412, alpha: 1), logo:
#imageLiteral(resourceName: "logo")) else { return }

imageView.image = qrURLImage

}
}

https://stackoverflow.com/questions/57703239/how-to-generate-a-qr-image-in-ios-app-using-swift 2/6
17/07/2024, 19:43 How to generate a QR image in iOS app using swift - Stack Overflow

extension URL {

/// Creates a QR code for the current URL in the given color.
func qrImage(using color: UIColor, logo: UIImage? = nil) -> UIImage? {

guard let tintedQRImage = qrImage?.tinted(using: color) else {


return nil
}

guard let logo = logo?.cgImage else {


return UIImage(ciImage: tintedQRImage)
}

guard let final = tintedQRImage.combined(with: CIImage(cgImage: logo))


else {
return UIImage(ciImage: tintedQRImage)
}

return UIImage(ciImage: final)


}

/// Returns a black and white QR code for this URL.


var qrImage: CIImage? {
guard let qrFilter = CIFilter(name: "CIQRCodeGenerator") else { return nil
}
let qrData = absoluteString.data(using: String.Encoding.ascii)
qrFilter.setValue(qrData, forKey: "inputMessage")

let qrTransform = CGAffineTransform(scaleX: 12, y: 12)


return qrFilter.outputImage?.transformed(by: qrTransform)
}
}

extension CIImage {
/// Inverts the colors and creates a transparent image by converting the mask
to alpha.
/// Input image should be black and white.
var transparent: CIImage? {
return inverted?.blackTransparent
}

/// Inverts the colors.


var inverted: CIImage? {
guard let invertedColorFilter = CIFilter(name: "CIColorInvert") else {

https://stackoverflow.com/questions/57703239/how-to-generate-a-qr-image-in-ios-app-using-swift 3/6
17/07/2024, 19:43 How to generate a QR image in iOS app using swift - Stack Overflow
return nil }

invertedColorFilter.setValue(self, forKey: "inputImage")


return invertedColorFilter.outputImage
}

/// Converts all black to transparent.


var blackTransparent: CIImage? {
guard let blackTransparentFilter = CIFilter(name: "CIMaskToAlpha") else {
return nil }
blackTransparentFilter.setValue(self, forKey: "inputImage")
return blackTransparentFilter.outputImage
}

/// Applies the given color as a tint color.


func tinted(using color: UIColor) -> CIImage? {
guard
let transparentQRImage = transparent,
let filter = CIFilter(name: "CIMultiplyCompositing"),
let colorFilter = CIFilter(name: "CIConstantColorGenerator") else {
return nil }

let ciColor = CIColor(color: color)


colorFilter.setValue(ciColor, forKey: kCIInputColorKey)
let colorImage = colorFilter.outputImage

filter.setValue(colorImage, forKey: kCIInputImageKey)


filter.setValue(transparentQRImage, forKey: kCIInputBackgroundImageKey)

return filter.outputImage!
}
}

extension CIImage {

/// Combines the current image with the given image centered.
func combined(with image: CIImage) -> CIImage? {
guard let combinedFilter = CIFilter(name: "CISourceOverCompositing") else {
return nil }
let centerTransform = CGAffineTransform(translationX: extent.midX -
(image.extent.size.width / 2), y: extent.midY - (image.extent.size.height / 2))
combinedFilter.setValue(image.transformed(by: centerTransform), forKey:
"inputImage")
combinedFilter.setValue(self, forKey: "inputBackgroundImage")
return combinedFilter.outputImage!

https://stackoverflow.com/questions/57703239/how-to-generate-a-qr-image-in-ios-app-using-swift 4/6
17/07/2024, 19:43 How to generate a QR image in iOS app using swift - Stack Overflow
}
}

Github Repo : https://github.com/bestiosdeveloper/QACodeDemo


Share Improve this answer Follow edited Dec 14, 2022 at 4:43 answered Aug 29, 2019 at 5:47
Pramod Kumar
2,527 1 12 28
For live demo project you can find: github.com/bestiosdeveloper/QACodeDemo – Pramod Kumar Aug 29, 2019 at 5:48
thanks for the answer firstly. one last thing i want to ask how will i shows those dots like in sample image. – Hasnain ahmad Aug 29, 2019 at 6:11
Thanks @bestiosdeveloper you save my time. – Rajan Singh Aug 30, 2019 at 10:43

func generateQRCode(from string: String) -> UIImage? {


let data = string.data(using: .ascii, allowLossyConversion:

4 false)
let filter = CIFilter(name: "CIQRCodeGenerator")
filter?.setValue(data, forKey: "inputMessage")
let img = UIImage(cgImage: CIContext(options:
nil).createCGImage((filter?.outputImage!)!, from:
(filter?.outputImage!.extent)!)!)
return img
}

Share Improve this answer Follow answered Aug 29, 2019 at 5:09
chinmai
59 7

You could achieve the same using CIFilter, CGAffineTransform https://developer.apple.com/documentation/coreimage/cifilter


https://developer.apple.com/documentation/coregraphics/cgaffinetransform
3 Sample code
https://stackoverflow.com/questions/57703239/how-to-generate-a-qr-image-in-ios-app-using-swift 5/6
17/07/2024, 19:43 How to generate a QR image in iOS app using swift - Stack Overflow

func genQRCode(from input: String) -> UIImage? {

let data = input.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?.transformed(by: transform) {


return UIImage(ciImage: output)
}
}

return nil
}

let image = generateQRCode(from: "Sample code to generate QR")

Share Improve this answer Follow answered Aug 29, 2019 at 5:06
Srikanth Balaji
2,688 4 20 32

https://stackoverflow.com/questions/57703239/how-to-generate-a-qr-image-in-ios-app-using-swift 6/6

You might also like