Created
April 16, 2018 05:27
-
-
Save hhyyg/f9f5ac7a540c66f40fefd24cb19cc3ef to your computer and use it in GitHub Desktop.
Generate QR Code for iOS
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
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
Eq エンジニアブログ: iOS - QRコードの読み取り/生成 (iOS11)