-
Notifications
You must be signed in to change notification settings - Fork 0
/
CodeType.swift
48 lines (43 loc) · 1.28 KB
/
CodeType.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// CodeType.swift
// QRCodeGenerator
//
// Created by N A Shashank on 12/13/17.
// Copyright © 2017 N A Shashank. All rights reserved.
//
import UIKit
enum CodeType{
case barcode
case qrcode
private func filterName() -> String
{
var strTemp = String()
switch self
{
case CodeType.barcode:
strTemp = "CICode128BarcodeGenerator"
case CodeType.qrcode:
strTemp = "CIQRCodeGenerator"
}
return strTemp
}
func image(string:String,imgViewFrame:CGRect) -> UIImage?
{
let data = string.data(using: String.Encoding.utf8)
guard let filter = CIFilter(name: self.filterName()) else{
return nil
}
filter.setValue(data, forKey: "inputMessage")
if self == CodeType.qrcode
{
filter.setValue("H", forKey: "inputCorrectionLevel")
}
guard let imgCode = filter.outputImage else{
return nil
}
let scaleX = imgViewFrame.size.width / imgCode.extent.size.width
let scaleY = imgViewFrame.size.height / imgCode.extent.size.height
let transform = CGAffineTransform(scaleX: scaleX, y: scaleY)
return UIImage(ciImage:imgCode.transformed(by: transform))
}
}