Created
October 12, 2016 12:33
-
-
Save weverb2/a5ba1f111beb1d546753775461b2ea9f to your computer and use it in GitHub Desktop.
Generate Barcode from a string
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 UIKit | |
class Barcode { | |
class func fromString(string : String, barcodeMode: BarcodeMode) -> UIImage? { | |
let data = string.dataUsingEncoding(NSASCIIStringEncoding) | |
let filter = CIFilter(name: barcodeMode.filterName) | |
filter?.setValue(data, forKey: "inputMessage") | |
guard let outputImage = filter?.outputImage else { | |
return UIImage() | |
} | |
let transform = CGAffineTransformMakeScale(5.0, 5.0) | |
let output = outputImage.imageByApplyingTransform(transform) | |
let context = CIContext(options: nil) | |
let newImage = UIImage (CGImage: context.createCGImage(output, fromRect: output.extent)) | |
return newImage | |
} | |
} | |
enum BarcodeMode { | |
case Barcode | |
case QRCode | |
var filterName :String { | |
switch self{ | |
case .Barcode: | |
return "CICode128BarcodeGenerator" | |
case .QRCode: | |
return "CIQRCodeGenerator" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment