iOS通过CIFilter生成条形码和二维码(Swift3.0)

iOS通过CIFilter生成条形码和二维码(Swift3.0)_第1张图片
Simulator Screen Shot002.png
iOS7之后,可以使用原生的CIFilter创建二维码,以下是实现代码:
public extension UIImage {

    /// 创建带logo的二维码图片
    ///
    /// - Parameters:
    ///   - content: 二维码内容信息
    ///   - iconName: logo 图片名称
    /// - Returns: UIImage?
    class func creat_qRCodeImage(content: String?, iconName: String?) -> UIImage? {
        
        let filter = CIFilter(name: "CIQRCodeGenerator")
        filter?.setDefaults()
        /// allowLossyConversion 在转换过程中是否可以移除或者改变字符
        let data = content?.data(using: String.Encoding.utf8, allowLossyConversion: false)
        filter?.setValue(data, forKey: "inputMessage")
        /// L M Q H 修正等级,应该跟采样有关
        filter?.setValue("H", forKey: "inputCorrectionLevel")
        let outputImage = filter?.outputImage
        // 创建一个颜色滤镜,黑白色
        let colorFilter = CIFilter(name: "CIFalseColor")!
        colorFilter.setDefaults()
        colorFilter.setValue(outputImage, forKey: "inputImage")
        colorFilter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
        colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1, alpha: 0), forKey: "inputColor1")
        // 返回二维码image
        let codeImage = UIImage(ciImage: (colorFilter.outputImage!.applying(CGAffineTransform(scaleX: 10, y: 10))))
        // 中间一般放logo
        if let icon_name = iconName {
            
            if let iconImage = UIImage(named: icon_name) {
                let rect = CGRect(x: 0, y: 0, width: codeImage.size.width, height: codeImage.size.height)
                UIGraphicsBeginImageContextWithOptions(rect.size, false, UIScreen.main.scale)
                UIGraphicsBeginImageContext(rect.size)
                codeImage.draw(in: rect)
                let avatarSize = CGSize(width: rect.size.width*0.15, height: rect.size.height*0.15)
                
                let x = (rect.width - avatarSize.width) * 0.5
                let y = (rect.height - avatarSize.height) * 0.5
                iconImage.draw(in: CGRect(x: x, y: y, width: avatarSize.width, height: avatarSize.height))
                
                let resultImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                return resultImage
            }
        }
        return codeImage
    }
}
iOS8之后,可以使用原生的CIFilter创建条形码,以下是实现代码:
public extension UIImage {

    /// 创建指定大小的的条形码图片
    ///
    /// - Parameters:
    ///   - content: 条形码内容信息
    ///   - size: 条形码大小
    /// - Returns: UIImage?
    class func creat_barCode(content: String?, size: CGSize) -> UIImage? {
        
        // iOS 8.0以上的系统才支持条形码的生成,iOS8.0以下需要使用第三方控件生成
        if #available(iOS 8.0, *) {
            // 注意生成条形码的编码方式
            let data = content?.data(using: String.Encoding.ascii, allowLossyConversion: false)
            let filter = CIFilter(name: "CICode128BarcodeGenerator")
            filter?.setValue(data, forKey: "inputMessage")
            filter?.setValue(NSNumber(value: 0), forKey: "inputQuietSpace")
            let outputImage = filter?.outputImage
            // 创建一个颜色滤镜,黑白色
            let colorFilter = CIFilter(name: "CIFalseColor")!
            colorFilter.setDefaults()
            colorFilter.setValue(outputImage, forKey: "inputImage")
            colorFilter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
            colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1, alpha: 0), forKey: "inputColor1")
            // 返回条形码image
            let codeImage = UIImage(ciImage: (colorFilter.outputImage!.applying(CGAffineTransform(scaleX: 10, y: 10))))
            return self.resize_image(image: codeImage, size: size)
        }else {
            return nil
        }
    }
}
结束

你可能感兴趣的:(iOS通过CIFilter生成条形码和二维码(Swift3.0))