Skip to content

Instantly share code, notes, and snippets.

@hhyyg
Created April 16, 2018 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhyyg/f9f5ac7a540c66f40fefd24cb19cc3ef to your computer and use it in GitHub Desktop.
Save hhyyg/f9f5ac7a540c66f40fefd24cb19cc3ef to your computer and use it in GitHub Desktop.
Generate QR Code for iOS
import CoreImage
import UIKit
static func generateQRCode(url: String) -> UIImage? {
let data = url.data(using: .utf8)!
let params: [String: Any] = [
"inputMessage": data,
"inputCorrectionLevel": "L"
]
let filter = CIFilter(name: "CIQRCodeGenerator", withInputParameters: params)
guard let image = filter?.outputImage else {
return nil
}
guard let cgImage = CIContext().createCGImage(image, from: image.extent) else {
return nil
}
let size = CGSize(width: 150, height: 150)
UIGraphicsBeginImageContextWithOptions(size, true, 0)
let ctx = UIGraphicsGetCurrentContext()
ctx?.interpolationQuality = .none
ctx?.draw(cgImage, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
let resized = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return resized
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment