当前位置: > > > Swift - 使用CIFilter生成二维码图片(QRCode)

Swift - 使用CIFilter生成二维码图片(QRCode)

(本文代码已升级至Swift3)

CIFilter提供了各种各样的滤镜,其中CIQRCodeGenerator可以用来生成二维码。下面通过一个样例演示如何将字符串(链接也是字符串)生成二维码图片,同时支持在二维码中间放置个性化图标。

效果图如下:
代码如下:
import UIKit

class ViewController: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //不带图片的二维码图片
        let imageViewIcon1 = UIImageView(frame: CGRect(x:20, y:20, width:150, height:150))
        imageViewIcon1.image = createQRForString(qrString: "http://www.hangge.com",
                                                 qrImageName: "")
        self.view.addSubview(imageViewIcon1)
        
        //带图片的二维码图片
        let imageViewIcon2 = UIImageView(frame: CGRect(x:200, y:20, width:150, height:150))
        imageViewIcon2.image = createQRForString(qrString: "http://www.hangge.com",
                                                 qrImageName: "logo")
        self.view.addSubview(imageViewIcon2)
    }
    
    //创建二维码图片
    func createQRForString(qrString: String?, qrImageName: String?) -> UIImage?{
        if let sureQRString = qrString {
            let stringData = sureQRString.data(using: .utf8,
                                               allowLossyConversion: false)
            // 创建一个二维码的滤镜
            let qrFilter = CIFilter(name: "CIQRCodeGenerator")!
            qrFilter.setValue(stringData, forKey: "inputMessage")
            qrFilter.setValue("H", forKey: "inputCorrectionLevel")
            let qrCIImage = qrFilter.outputImage
            
            // 创建一个颜色滤镜,黑白色
            let colorFilter = CIFilter(name: "CIFalseColor")!
            colorFilter.setDefaults()
            colorFilter.setValue(qrCIImage, forKey: "inputImage")
            colorFilter.setValue(CIColor(red: 0, green: 0, blue: 0), forKey: "inputColor0")
            colorFilter.setValue(CIColor(red: 1, green: 1, blue: 1), forKey: "inputColor1")
            
            // 返回二维码image
            let codeImage = UIImage(ciImage: colorFilter.outputImage!
                .applying(CGAffineTransform(scaleX: 5, y: 5)))
            
            // 通常,二维码都是定制的,中间都会放想要表达意思的图片
            if let iconImage = UIImage(named: qrImageName!) {
                let rect = CGRect(x:0, y:0, width:codeImage.size.width,
                                  height:codeImage.size.height)
                UIGraphicsBeginImageContext(rect.size)
                
                codeImage.draw(in: rect)
                let avatarSize = CGSize(width:rect.size.width * 0.25,
                                        height:rect.size.height * 0.25)
                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
        }
        return nil
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
}
评论3
  • 3楼
    2017-08-01 10:27
    111

    let codeimage = UIImage(CIImage: colorFilter.outputImage!.imageByApplyingTransform(CGAffineTransformMakeScale(5, 5))) z这个方法报错!

    站长回复

    多谢你的提醒,代码现已更新,你可以再看下。

  • 2楼
    2017-04-12 22:19
    余欢

    swift3.0报错

    站长回复

    代码已更新,你可以再看下。

  • 1楼
    2015-11-30 14:25
    duofai

    真的很实用的功能,我几乎每天都上这个网站来学习东西,感谢站长一直以来的辛苦付出,赞

    站长回复

    很高兴写的这些文章能让你有所收获,欢迎常来看看。