Skip to content

Instantly share code, notes, and snippets.

@ha1f
Last active December 13, 2017 05:08
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 ha1f/aa65ac292e5daef996a17a41826ccec3 to your computer and use it in GitHub Desktop.
Save ha1f/aa65ac292e5daef996a17a41826ccec3 to your computer and use it in GitHub Desktop.
QR Code Generator
//
// QrCodeGenerator.swift
// CoreImageSample
//
// Created by はるふ on 2017/12/11.
// Copyright © 2017年 ha1f. All rights reserved.
//
import Foundation
import CoreImage
import UIKit
/// QR Code Generator using CIFilter
/// https://developer.apple.com/library/content/documentation/GraphicsImaging/Reference/CoreImageFilterReference/index.html#//apple_ref/doc/filter/ci/CIQRCodeGenerator
struct QrCodeGenerator {
enum InputCorrectionLevel: String {
/// 7%
case L = "L"
/// 15%
case M = "M"
/// 25%
case Q = "Q"
/// 30%
case H = "H"
}
let inputCorrectionLevel: InputCorrectionLevel = .M
func generate(fromData data: Data) -> CIImage? {
let filter = CIFilter.qRCodeGenerator(inputMessage: data as NSData, inputCorrectionLevel: inputCorrectionLevel.rawValue as NSString)
return filter?.outputImage
}
func generate(fromData data: Data, imageWidth: CGFloat) -> CIImage? {
guard let image = generate(fromData: data) else {
return nil
}
let scale = imageWidth / image.extent.size.width
return image.transformed(by: CGAffineTransform(scaleX: scale, y: scale))
}
func generate(fromString string: String, imageWidth: CGFloat) -> CIImage? {
let data = string.data(using: .isoLatin1)
return data.flatMap { self.generate(fromData: $0, imageWidth: imageWidth) }
}
func generateUiImage(fromString string: String, imageWidth: CGFloat, scale: CGFloat = UIScreen.main.scale) -> UIImage? {
return generate(fromString: string, imageWidth: imageWidth * scale)
.map { ciImage in
return UIImage(ciImage: ciImage, scale: scale, orientation: .up)
}
}
}
@ha1f
Copy link
Author

ha1f commented Dec 11, 2017

Sample usage

//
//  ViewController.swift
//  CoreImageSample
//
//  Created by はるふ on 2017/12/11.
//  Copyright © 2017年 ha1f. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    let imageView: UIImageView = {
       let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        return imageView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(imageView)
        imageView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            imageView.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            imageView.widthAnchor.constraint(equalToConstant: 256),
            imageView.heightAnchor.constraint(equalToConstant: 256)
            ])
        
        imageView.image = QrCodeGenerator().generateUiImage(fromString: "https://ha1f.net", imageWidth: 256)
    }
}

@ha1f
Copy link
Author

ha1f commented Dec 11, 2017

scaleが考慮されないので注意

@ha1f
Copy link
Author

ha1f commented Dec 13, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment