Skip to content

Instantly share code, notes, and snippets.

@madcato
Created March 12, 2018 08:13
Show Gist options
  • Save madcato/f0e90dcff756872b8bd0bb8607dded15 to your computer and use it in GitHub Desktop.
Save madcato/f0e90dcff756872b8bd0bb8607dded15 to your computer and use it in GitHub Desktop.
Show a QR in a view
//
// QRHelperViewController.swift
// QRHelper
//
// Created by Daniel Vela on 05/12/2017.
// Copyright © 2017 veladan. All rights reserved.
//
// Doc: https://www.unicorn-it.de/developing-reusable-views-in-a-swift-framework/
// Doc: https://www.appcoda.com/qr-code-generator-tutorial/
@objc public protocol QRViewDataSource {
func qrValueToShow() -> String
}
/*!
View to shwo a QR
In orther to use this class, you must include in your project a xib called
QRHelperView. In this xib include a view with two controls: a UILabel called qrLabel
and a UIImageView called qrImage.
Also to this this class you must implemente de protocol QRViewDataSource in order
to set the QR value to show.
*/
@IBDesignable
public class QRHelperView: UIView {
@IBOutlet var dataSource: QRViewDataSource? {
didSet {
qrLabel.text = dataSource?.qrValueToShow()
qrImage.image = qrImage.image ?? makeQrImage(text: qrLabel.text)
}
}
@IBOutlet weak var qrLabel: UILabel!
@IBOutlet weak var qrImage: UIImageView!
let nibName: String = "QRHelperView"
var view: UIView!
func setup() {
self.view = UINib(nibName: self.nibName, bundle:
Bundle(for: type(of: self))).instantiate(withOwner: self, options: nil)[0] as? UIView
self.view.frame = bounds
self.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
self.addSubview(self.view)
}
public override init(frame: CGRect) {
super.init(frame: frame)
setup()
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
setup()
}
private func makeQrImage(text: String?) -> UIImage? {
let data = text?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false)
if let filter = CIFilter(name: "CIQRCodeGenerator") {
filter.setValue(data, forKey: "inputMessage")
filter.setValue("Q", forKey: "inputCorrectionLevel")
if let ciImage = filter.outputImage {
let scaleX = self.frame.size.width / ciImage.extent.size.width
let scaleY = self.frame.size.height / ciImage.extent.size.height
let transformedImage = ciImage.transformed(by: CGAffineTransform(scaleX: scaleX, y: scaleY))
return UIImage(ciImage: transformedImage)
}
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment