二维码---生成 swift

swift 3.0 Xcode 8.1

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        let label = UILabel.init(frame: CGRect.init(x: 50, y: 100, width: 200, height: 21))
        label.text = "my name is Apple"
        self.view.addSubview(label)
        
        let imageView = UIImageView.init(frame: CGRect.init(x: 50, y: 200, width: 300, height: 300))
        imageView.backgroundColor = UIColor.gray
        self.view.addSubview(imageView)
        
        //添加二维码图片
        imageView.image = self.creatQRCodeImage(text: label.text!)
    }

    //MARK: -传进去字符串,生成二维码图片
    func creatQRCodeImage(text: String) -> UIImage{
        
        //创建滤镜
        let filter = CIFilter(name: "CIQRCodeGenerator")
        //还原滤镜的默认属性
        filter?.setDefaults()
        //设置需要生成二维码的数据
        filter?.setValue(text.data(using: String.Encoding.utf8), forKey: "inputMessage")
        //从滤镜中取出生成的图片
        let ciImage = filter?.outputImage
        //把CIImage转成UIImage
        //let bgImage = UIImage.init(ciImage: ciImage!) //这个清晰度不好
        let bgImage = createNonInterpolatedUIImageFormCIImage(image: ciImage!, size: 300) //这个清晰度好
        //创建一个头像
        let icon = UIImage(named: "123.jpg")
        //合成图片(把二维码和头像合并)
        let newImage = creatImage(bgImage: bgImage, iconImage: icon!)
        //返回生成好的二维码
        return newImage
    }
    
    //MARK: - 根据CIImage生成指定大小的高清UIImage
    func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        //CIImage没有frame与bounds属性,只有extent属性
        let ciextent: CGRect = image.extent.integral
        let scale: CGFloat = min(size/ciextent.width, size/ciextent.height)
        
        let context = CIContext(options: nil)  //创建基于GPU的CIContext对象,性能和效果更好
        let bitmapImage: CGImage = context.createCGImage(image, from: ciextent)! //CIImage->CGImage
        
        let width = ciextent.width * scale
        let height = ciextent.height * scale
        let cs: CGColorSpace = CGColorSpaceCreateDeviceGray() //灰度颜色通道
        let info_UInt32 = CGImageAlphaInfo.none.rawValue
        
        let bitmapRef = CGContext(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: info_UInt32)! //图形上下文,画布
        bitmapRef.interpolationQuality = CGInterpolationQuality.none //写入质量
        bitmapRef.scaleBy(x: scale, y: scale) //调整“画布”的缩放
        bitmapRef.draw(bitmapImage, in: ciextent)  //绘制图片
        
        let scaledImage: CGImage = bitmapRef.makeImage()! //保存
        return UIImage(cgImage: scaledImage)
    }
    
    //MARK: - 根据背景图片和头像合成头像二维码
    func creatImage(bgImage: UIImage, iconImage:UIImage) -> UIImage{
        
        //开启图片上下文
        UIGraphicsBeginImageContext(bgImage.size)
        //绘制背景图片
        bgImage.draw(in: CGRect(origin: CGPoint.zero, size: bgImage.size))
        //绘制头像
        let width: CGFloat = 50
        let height: CGFloat = width
        let x = (bgImage.size.width - width) * 0.5
        let y = (bgImage.size.height - height) * 0.5
        iconImage.draw(in: CGRect(x: x, y: y, width: width, height: height))
        //取出绘制好的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        //关闭上下文
        UIGraphicsEndImageContext()
        //返回合成好的图片
        return newImage!
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
相关文章
相关标签/搜索