Generate QR Code Swift 4.2
Example code QR code
1. Simple Generate QR Code
// @IBAction func bAction(_ sender: Any) {
// if let myString = textField.text {
// // first
//
// // second
// let data = myString.data(using: .ascii, allowLossyConversion: false)
// guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return }
// filter.setValue(data, forKey: "inputMessage")
//
// let qrTransform = CGAffineTransform(scaleX: 12, y: 12)
// guard let qrImage = filter.outputImage?.transformed(by: qrTransform) else { return }
// let img = UIImage(ciImage: qrImage)
// imageView.image = img
// }
// }
2. Generate Qr Code with Image (image inside QR Code)
// @IBAction func bAction(_ sender: Any) {
// if let myString = textField.text {
// let data = myString.data(using: .ascii, allowLossyConversion: false)
// guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return }
// filter.setValue(data, forKey: "inputMessage")
// filter.setValue("Q", forKey: "inputCorrectionLevel")
//
// qrCodeImage = filter.outputImage
//
// let scaleX = imageView.frame.size.width / qrCodeImage.extent.size.width
// let scaleY = imageView.frame.size.height / qrCodeImage.extent.size.height
//
// let transformedImage = qrCodeImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
// imageView.image = UIImage(ciImage: transformedImage)
//
// // mini image
// let image = UIImage(named: "smallImage")
// let overlayImageView = UIImageView(image: image)
// overlayImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
// overlayImageView.translatesAutoresizingMaskIntoConstraints = false
// overlayImageView.clipsToBounds = true
// overlayImageView.contentMode = UIView.ContentMode.scaleAspectFit
//
// // subView
// let subview = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
// subview.backgroundColor = UIColor.white
// subview.layer.cornerRadius = subview.frame.width / 2
// subview.layer.borderColor = UIColor.gray.cgColor
// subview.layer.borderWidth = 2
// subview.center = CGPoint(x: imageView.frame.size.width / 2, y: imageView.frame.size.height / 2)
//
// subview.addSubview(overlayImageView)
//
// imageView.addSubview(subview)
//
// // constraint image
//
// let centerXConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerX, relatedBy: .equal, toItem: subview, attribute: .centerX, multiplier: 1, constant: 0)
// let centerYConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerY, relatedBy: .equal, toItem: subview, attribute: .centerY, multiplier: 1, constant: 0)
// NSLayoutConstraint.activate([centerXConst, centerYConst])
//
// // constraint view
//
// let centerXCons = NSLayoutConstraint(item: subview, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0)
// let centerYCons = NSLayoutConstraint(item: subview, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0)
// NSLayoutConstraint.activate([centerXCons, centerYCons])
// }
// }
Full code:
import UIKit
class GenerateQrCodeViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var textField: UITextField!
var qrCodeImage: CIImage!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func bAction(_ sender: Any) {
if let myString = textField.text {
let data = myString.data(using: .ascii, allowLossyConversion: false)
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return }
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
qrCodeImage = filter.outputImage
let scaleX = imageView.frame.size.width / qrCodeImage.extent.size.width
let scaleY = imageView.frame.size.height / qrCodeImage.extent.size.height
let transformedImage = qrCodeImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
imageView.image = UIImage(ciImage: transformedImage)
// mini image
let image = UIImage(named: "smallImage")
let overlayImageView = UIImageView(image: image)
overlayImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
overlayImageView.translatesAutoresizingMaskIntoConstraints = false
overlayImageView.clipsToBounds = true
overlayImageView.contentMode = UIView.ContentMode.scaleAspectFit
// subView
let subview = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
subview.backgroundColor = UIColor.white
subview.layer.cornerRadius = subview.frame.width / 2
subview.layer.borderColor = UIColor.gray.cgColor
subview.layer.borderWidth = 2
subview.center = CGPoint(x: imageView.frame.size.width / 2, y: imageView.frame.size.height / 2)
subview.addSubview(overlayImageView)
imageView.addSubview(subview)
// constraint image
let centerXConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerX, relatedBy: .equal, toItem: subview, attribute: .centerX, multiplier: 1, constant: 0)
let centerYConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerY, relatedBy: .equal, toItem: subview, attribute: .centerY, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([centerXConst, centerYConst])
// constraint view
let centerXCons = NSLayoutConstraint(item: subview, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0)
let centerYCons = NSLayoutConstraint(item: subview, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([centerXCons, centerYCons])
}
}
}
1. Simple Generate QR Code
// @IBAction func bAction(_ sender: Any) {
// if let myString = textField.text {
// // first
//
// // second
// let data = myString.data(using: .ascii, allowLossyConversion: false)
// guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return }
// filter.setValue(data, forKey: "inputMessage")
//
// let qrTransform = CGAffineTransform(scaleX: 12, y: 12)
// guard let qrImage = filter.outputImage?.transformed(by: qrTransform) else { return }
// let img = UIImage(ciImage: qrImage)
// imageView.image = img
// }
// }
2. Generate Qr Code with Image (image inside QR Code)
// @IBAction func bAction(_ sender: Any) {
// if let myString = textField.text {
// let data = myString.data(using: .ascii, allowLossyConversion: false)
// guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return }
// filter.setValue(data, forKey: "inputMessage")
// filter.setValue("Q", forKey: "inputCorrectionLevel")
//
// qrCodeImage = filter.outputImage
//
// let scaleX = imageView.frame.size.width / qrCodeImage.extent.size.width
// let scaleY = imageView.frame.size.height / qrCodeImage.extent.size.height
//
// let transformedImage = qrCodeImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
// imageView.image = UIImage(ciImage: transformedImage)
//
// // mini image
// let image = UIImage(named: "smallImage")
// let overlayImageView = UIImageView(image: image)
// overlayImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
// overlayImageView.translatesAutoresizingMaskIntoConstraints = false
// overlayImageView.clipsToBounds = true
// overlayImageView.contentMode = UIView.ContentMode.scaleAspectFit
//
// // subView
// let subview = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
// subview.backgroundColor = UIColor.white
// subview.layer.cornerRadius = subview.frame.width / 2
// subview.layer.borderColor = UIColor.gray.cgColor
// subview.layer.borderWidth = 2
// subview.center = CGPoint(x: imageView.frame.size.width / 2, y: imageView.frame.size.height / 2)
//
// subview.addSubview(overlayImageView)
//
// imageView.addSubview(subview)
//
// // constraint image
//
// let centerXConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerX, relatedBy: .equal, toItem: subview, attribute: .centerX, multiplier: 1, constant: 0)
// let centerYConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerY, relatedBy: .equal, toItem: subview, attribute: .centerY, multiplier: 1, constant: 0)
// NSLayoutConstraint.activate([centerXConst, centerYConst])
//
// // constraint view
//
// let centerXCons = NSLayoutConstraint(item: subview, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0)
// let centerYCons = NSLayoutConstraint(item: subview, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0)
// NSLayoutConstraint.activate([centerXCons, centerYCons])
// }
// }
Full code:
import UIKit
class GenerateQrCodeViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var textField: UITextField!
var qrCodeImage: CIImage!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func bAction(_ sender: Any) {
if let myString = textField.text {
let data = myString.data(using: .ascii, allowLossyConversion: false)
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { return }
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
qrCodeImage = filter.outputImage
let scaleX = imageView.frame.size.width / qrCodeImage.extent.size.width
let scaleY = imageView.frame.size.height / qrCodeImage.extent.size.height
let transformedImage = qrCodeImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
imageView.image = UIImage(ciImage: transformedImage)
// mini image
let image = UIImage(named: "smallImage")
let overlayImageView = UIImageView(image: image)
overlayImageView.frame = CGRect(x: 0, y: 0, width: 50, height: 50)
overlayImageView.translatesAutoresizingMaskIntoConstraints = false
overlayImageView.clipsToBounds = true
overlayImageView.contentMode = UIView.ContentMode.scaleAspectFit
// subView
let subview = UIView(frame: CGRect(x: 0, y: 0, width: 60, height: 60))
subview.backgroundColor = UIColor.white
subview.layer.cornerRadius = subview.frame.width / 2
subview.layer.borderColor = UIColor.gray.cgColor
subview.layer.borderWidth = 2
subview.center = CGPoint(x: imageView.frame.size.width / 2, y: imageView.frame.size.height / 2)
subview.addSubview(overlayImageView)
imageView.addSubview(subview)
// constraint image
let centerXConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerX, relatedBy: .equal, toItem: subview, attribute: .centerX, multiplier: 1, constant: 0)
let centerYConst = NSLayoutConstraint(item: overlayImageView, attribute: .centerY, relatedBy: .equal, toItem: subview, attribute: .centerY, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([centerXConst, centerYConst])
// constraint view
let centerXCons = NSLayoutConstraint(item: subview, attribute: .centerX, relatedBy: .equal, toItem: imageView, attribute: .centerX, multiplier: 1, constant: 0)
let centerYCons = NSLayoutConstraint(item: subview, attribute: .centerY, relatedBy: .equal, toItem: imageView, attribute: .centerY, multiplier: 1, constant: 0)
NSLayoutConstraint.activate([centerXCons, centerYCons])
}
}
}
No comments: