美文网首页
[Swift] 生成二维码

[Swift] 生成二维码

作者: 巨馍蘸酱 | 来源:发表于2023-03-27 14:17 被阅读0次
        //创建二维码图片
        func createQRForString(qrString: String?, qrImage: UIImage?) -> 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)))
                let codeImage = UIImage(ciImage: colorFilter.outputImage!.transformed(by: CGAffineTransform(scaleX: 5, y: 5)))
                
                // 通常,二维码都是定制的,中间都会放想要表达意思的图片
                if let iconImage = qrImage {
                    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
        }
    

    相关文章

      网友评论

          本文标题:[Swift] 生成二维码

          本文链接:https://www.haomeiwen.com/subject/pgjnrdtx.html