Skip to content

Instantly share code, notes, and snippets.

@fitomad fitomad/URL+QR.swift
Created Mar 29, 2019

Embed
What would you like to do?
URL extension to generate a QR code from the URL itself. Ready for Swift 5
//
// URL+QR.swift
// URL extension to generate QR code from an URL
//
// Created by Adolfo Vera Blasco on 29/03/2019.
// Copyright © 2019 Adolfo Vera Blasco. All rights reserved.
//
import UIKit
import CoreImage
import Foundation
extension URL
{
// Código QR de la imagen
var qrImage: UIImage?
{
return generateQR()
}
/**
Crea un código QR de esta `URL`
*/
func generateQR() -> UIImage?
{
guard let data = self.absoluteString.data(using: .isoLatin1),
let qrFilter = CIFilter(name: "CIQRCodeGenerator")
else
{
return nil
}
qrFilter.setValue(data, forKey: "inputMessage")
if var qrCIImage = qrFilter.outputImage
{
let transformation = CGAffineTransform(scaleX: 20.0, y: 20.0)
qrCIImage = qrCIImage.transformed(by: transformation)
let context = CIContext(options: nil)
let cgImage = context.createCGImage(qrCIImage, from: qrCIImage.extent)
return UIImage(cgImage: cgImage!)
}
return nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.