Swift快速集成函数使用篇(生成二维码)

前端之家收集整理的这篇文章主要介绍了Swift快速集成函数使用篇(生成二维码)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  • 1 代码生成二维码


    E312AE79-44A0-472E-938C-5599B04CA92A.png
  • 2 一行代码集成

    //返回UIImage
        CommonFunction.CreateQRCode("张三",qrImageName: "张三的图片,可为空")
  • 3 生成二维码代码

    func CreateQRCode(qrString: String?,qrImageName: String?) -> UIImage?{
          if let sureQRString = qrString {
              let stringData = sureQRString.dataUsingEncoding(NSUTF8StringEncoding,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!.imageByApplyingTransform(CGAffineTransformMakeScale(5,5)))
              // 通常,二维码都是定制的,中间都会放想要表达意思的图片
              if let iconImage = UIImage(named: qrImageName!) {
                  let rect = CGRectMake(0,codeImage.size.width,codeImage.size.height)
                  UIGraphicsBeginImageContext(rect.size)
                  codeImage.drawInRect(rect)
                  let avatarSize = CGSizeMake(rect.size.width * 0.25,rect.size.height * 0.25)
                  let x = (rect.width - avatarSize.width) * 0.5
                  let y = (rect.height - avatarSize.height) * 0.5
                  iconImage.drawInRect(CGRectMake(x,y,avatarSize.width,avatarSize.height))
                  let resultImage = UIGraphicsGetImageFromCurrentImageContext()
    
                  UIGraphicsEndImageContext()
                  return resultImage
              }
              return codeImage
          }
          return nil
      }




    文/Hcy_ 原文链接:http://www.jianshu.com/p/8872f96587e9
  • 猜你在找的Swift相关文章