Created
November 21, 2019 19:26
-
-
Save brunogama/178a2dfdc100c4bd21ca8c2a4cccd6e5 to your computer and use it in GitHub Desktop.
There's no need to download third party software when you can execute this code and generate your own qr-code generator qr-code.playground π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: A UIKit based Playground for presenting user interface | |
import UIKit | |
import PlaygroundSupport | |
final class QRCodeViewController: UIViewController { | |
private var input: String = "" | |
func generateQRCode(from string: String) -> UIImage? { | |
let data = string.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 | |
} | |
init(input: String) { | |
self.input = input | |
super.init(nibName: nil, bundle: nil) | |
} | |
@available(*, unavailable) | |
required init?(coder aDecoder: NSCoder) { | |
fatalError("init(coder:) has not been implemented") | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
self.view = UIImageView(image: generateQRCode(from: input)) | |
} | |
} | |
class MyViewController : UIViewController { | |
override func loadView() { | |
let view = UIView() | |
view.backgroundColor = .white | |
let qrCodeViewController = QRCodeViewController(input: "SPAM HAM EGGS") | |
qrCodeViewController.view.frame = CGRect(x: 0, y: 0, width: 300, height: 300) | |
addChild(qrCodeViewController) | |
view.addSubview(qrCodeViewController.view) | |
qrCodeViewController.didMove(toParent: self) | |
self.view = view | |
} | |
} | |
// Present the view controller in the Live View window | |
PlaygroundPage.current.liveView = MyViewController() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment