Created
June 8, 2019 12:12
-
-
Save ingconti/3286880dfe4ba926c73f5dc9eff04179 to your computer and use it in GitHub Desktop.
QRCodeImage for iOS and OSX: allows to create a correct QR code without blur (ported from https://gist.github.com/snej/c210cc4cbfe8fd277186)
This file contains 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
// | |
// QRImage.swift | |
// QRCodeSample | |
// | |
// Created by ing.conti on 08/06/2019. | |
// Copyright © 2019 ing.conti. All rights reserved. | |
// | |
#if os(iOS) | |
import UIKit | |
typealias QRImage = UIImage | |
#elseif os(OSX) | |
import Cocoa | |
typealias QRImage = NSImage | |
#endif | |
func QRCodeImageWith(string: String, size: CGFloat = 500) -> QRImage?{ | |
guard let data = string.data(using: .utf8) else { | |
return nil | |
} | |
return QRCodeImageWith(data: data, size: size) | |
} | |
func QRCodeImageWith(data: Data, size: CGFloat = 500) -> QRImage?{ | |
guard let filter = CIFilter(name: "CIQRCodeGenerator") else { | |
return nil | |
} | |
filter.setValue(data, forKey: "inputMessage") | |
guard let ciImage = filter.outputImage else { | |
return nil | |
} | |
let destRect = CGRect(x: 0, y: 0, width: size, height: size) | |
#if os(iOS) | |
let tinyImage = UIImage(ciImage: ciImage) | |
if (size <= tinyImage.size.width){ | |
return tinyImage | |
} | |
// Scale image up: | |
UIGraphicsBeginImageContext(CGSize(width: size, height: size)) | |
if let context = UIGraphicsGetCurrentContext(){ | |
context.interpolationQuality = .none | |
tinyImage.draw(in: destRect) | |
let image = UIGraphicsGetImageFromCurrentImageContext() | |
UIGraphicsEndImageContext() | |
return image | |
} | |
return nil | |
#elseif os(OSX) | |
let rep = NSCIImageRep(ciImage: ciImage) | |
let tinyImage = NSImage() | |
tinyImage.addRepresentation(rep) | |
if size <= rep.size.width { | |
return tinyImage as QRImage | |
} | |
// Scale image up: | |
let nsImage = NSImage(size: NSSize(width: size, height: size)) | |
nsImage.lockFocus() | |
let ctx = NSGraphicsContext.current | |
ctx?.imageInterpolation = NSImageInterpolation.none | |
tinyImage.draw(in: destRect) | |
nsImage.unlockFocus() | |
return nsImage | |
#endif | |
} | |
// SEE below for usage on iOS and OSX | |
This file contains 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
// | |
// ViewController.swift | |
// QRCodeSample | |
// | |
// Created by ing.conti on 25/05/2019. | |
// Copyright © 2019 ing.conti. All rights reserved. | |
// | |
import Cocoa | |
import CoreImage | |
class ViewController: NSViewController { | |
private var qrcodeImage: CIImage? | |
@IBOutlet weak var QRCodeImageView: NSImageView! | |
@IBOutlet weak var txt: NSTextField! | |
@IBAction func generate(_ sender: Any) { | |
if txt.stringValue == "" { | |
txt.stringValue = "hello" | |
} | |
QRCodeImageView.wantsLayer = true | |
guard let data = txt.stringValue.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) else { | |
return | |
} | |
let tempSwift = QRCodeImageWith(data: data, size: 300) | |
QRCodeImageView.image = tempSwift | |
return | |
} | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
generate(self) | |
} | |
override var representedObject: Any? { | |
didSet { | |
// Update the view, if already loaded. | |
} | |
} | |
} |
This file contains 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
// | |
// ViewController.swift | |
// QRCodeSampleiOS | |
// | |
// Created by ing.conti on 08/06/2019. | |
// Copyright © 2019 ing.conti. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var txt: UITextField! | |
@IBOutlet weak var QRCodeImageView: UIImageView! | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
if txt.text == "" { | |
txt.text = "hello" | |
} | |
generate(UIButton()) | |
} | |
@IBAction func generate(_ sender: UIButton) { | |
guard let data = txt.text?.data(using: String.Encoding.isoLatin1, allowLossyConversion: false) else { | |
return | |
} | |
let tempSwift = QRCodeImageWith(data: data, size: 300) | |
QRCodeImageView.image = tempSwift | |
return | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment