How To Generate A QR Image in iOS App Using Swift - Stack Overflow
How To Generate A QR Image in iOS App Using Swift - Stack Overflow
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.
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
Here is the code for generating the QR code with logo image in centre:
10 import UIKit
import EventKit
import CoreImage
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? {
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
}
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 }
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
}
}
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
return nil
}
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