方法1
class GenerateQRCode: NSObject {
static let shared = GenerateQRCode()
///生成二维码
func generateQRCodeImg(_ qrStr: String, qrImgName: String? = nil) -> UIImage {
// 1. 生成二维码
let qrFilter = CIFilter(name: "CIQRCodeGenerator")!
qrFilter.setDefaults()
qrFilter.setValue(qrStr.data(using: String.Encoding.utf8), forKey: "inputMessage")
let ciImage = qrFilter.outputImage
// 2. 缩放处理
let transform = CGAffineTransform(scaleX: 10, y: 10)
let transformImage = ciImage?.transformed(by: transform)
// 3. 颜色滤镜
let colorFilter = CIFilter(name: "CIFalseColor")!
colorFilter.setDefaults()
colorFilter.setValue(transformImage, forKey: "inputImage")
// 前景色
colorFilter.setValue(CIColor(color: UIColor.black), forKey: "inputColor0")
// 背景色
colorFilter.setValue(CIColor(color: UIColor.white), forKey: "inputColor1")
let outputImage = colorFilter.outputImage
var scanImg = UIImage.init(ciImage: outputImage!)
if qrImgName != nil {
scanImg = insertAvatarImage(qrimage: UIImage.init(ciImage: outputImage!), image: UIImage(named: qrImgName!)!)
}
return scanImg
}
func insertAvatarImage(qrimage: UIImage, image: UIImage) -> UIImage {
UIGraphicsBeginImageContext(qrimage.size)
let rect = CGRect(origin: CGPoint.zero, size: qrimage.size)
qrimage.draw(in: rect)
let w = rect.width * 0.2
let x = (rect.width - w) * 0.5
let y = (rect.height - w) * 0.5
image.draw(in: CGRect(x: x, y: y, width: w, height: w))
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
方法2
import UIKit
import Foundation
class QRCodeGenerateGold: NSObject {
/// 生成一张普通的二维码
///
/// - Parameters:
/// - qrcodeUrl: 要生成二维码的数据
/// - imageWidth: 图片的尺寸
/// - Returns: 普通的二维码 UIImage
class func generateNormalQRCode(_ qrcodeUrl: String, imageSize: CGSize) -> UIImage {
//创建滤镜对象
let filter = CIFilter.init(name: "CIQRCodeGenerator") ?? CIFilter.init()
//恢复滤镜的默认熟悉
filter.setDefaults()
//设置数据
let infoData = qrcodeUrl.data(using: String.Encoding.utf8)
// 通过KVC设置滤镜inputMessage数据
filter.setValue(infoData, forKey: "inputMessage")
//获得滤镜输出的图像
let outputImage = filter.outputImage ?? CIImage.init()
return self.createImageFromCIImage(outputImage, imageSize: imageSize)
}
/// 根据CIImage生成指定大小的高清UIImage
///
/// - Parameters:
/// - image: 滤镜图片 CIImage
/// - withSize: 尺寸
/// - Returns:根据CIImage生成指定大小的高清 UIImage
class func createImageFromCIImage(_ image: CIImage, imageSize: CGSize) -> UIImage {
let extent = image.extent.integral
let scale = min(imageSize.width / extent.width, imageSize.height / extent.height)
//创建bitmap
let width = extent.width * scale
let height = extent.height * scale
let cs = CGColorSpaceCreateDeviceGray()
let bitmapRef = CGContext.init(data: nil, width: Int(width), height: Int(height), bitsPerComponent: 8, bytesPerRow: 0, space: cs, bitmapInfo: 0)!
let contex = CIContext(options: nil)
let bitmapImage = contex.createCGImage(image, from: extent)!
bitmapRef.interpolationQuality = .none
bitmapRef.scaleBy(x: scale, y: scale)
bitmapRef.draw(bitmapImage, in: extent)
//保存bitmap到图片
var img = UIImage()
if let scaleImage = bitmapRef.makeImage() {
img = UIImage(cgImage: scaleImage)
}
return img
}
/// 生成一张带有logo的二维码
///
/// - Parameters:
/// - qrcodeUrl: 要生成二维码的数据
/// - logoImageName: logo的image名
/// - logoScale: logo相对于父视图的缩放比(取值范围:0-1,0,代表不显示,1,代表与父视图大小相同)
/// - Returns: 带有logo的二维码UIImage
class func generateLogoQRCode(_ qrcodeUrl: String, logoImageName: String, logoScale: CGFloat = 0.2) -> UIImage {
//创建滤镜对象
let filter = CIFilter.init(name: "CIQRCodeGenerator") ?? CIFilter.init()
//恢复滤镜的默认熟悉
filter.setDefaults()
//设置数据 (虽然二维码本质上是字符串, 但是这里需要转换, 不转换就崩溃)
let infoData = qrcodeUrl.data(using: String.Encoding.utf8)
// 通过KVC设置滤镜inputMessage数据
filter.setValue(infoData, forKey: "inputMessage")
//获得滤镜输出的图像
var outputImage = filter.outputImage ?? CIImage.init()
//图片小于(27,27),我们需要放大
outputImage = outputImage.transformed(by: CGAffineTransform.init(scaleX: 20, y: 20))
//将CIImage类型转成UIImage类型
let startImage = UIImage(ciImage: outputImage)
// - - - - - - - - - - - - - - - - 添加中间小图标 - - - - - - - - - - - - - - - -
//开启绘图, 获取图形上下文 (上下文的大小, 就是二维码的大小)
UIGraphicsBeginImageContext(startImage.size)
//把二维码图片画上去 (这里是以图形上下文, 左上角为(0,0)点
let size = CGRect.init(x: 0, y: 0, width: startImage.size.width, height: startImage.size.height)
//CGRect.init(origin: CGPoint.zero, size: startImage.size)
startImage.draw(in: size)
// 再把小图片画上去
let iconImage = UIImage.init(named: logoImageName) ?? UIImage()
let iconImageW = startImage.size.width * logoScale
let iconImageH = startImage.size.height * logoScale
let iconImageX = (startImage.size.width - iconImageW) * 0.5
let iconImageY = (startImage.size.height - iconImageH) * 0.5
let rect = CGRect.init(x: iconImageX, y: iconImageY, width: iconImageW, height: iconImageH)
iconImage.draw(in: rect)
//获取当前画得的这张图片
let finalImage = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage()
//关闭上下文
UIGraphicsEndPDFContext()
return finalImage
}
/// 生成一张彩色的二维码
///
/// - Parameters:
/// - qrcodeUrl: 要生成二维码的数据
/// - backgroundColor: 背景色
/// - mainColor: 主颜色
/// - Returns: 一张彩色的二维码UIImage
class func generateColorQRCode(_ qrcodeUrl: String, backgroundColor: UIColor, mainColor: UIColor) -> UIImage {
//创建滤镜对象
let filter = CIFilter.init(name: "CIQRCodeGenerator") ?? CIFilter.init()
//恢复滤镜的默认熟悉
filter.setDefaults()
//设置数据
let infoData = qrcodeUrl.data(using: String.Encoding.utf8)
// 通过KVC设置滤镜inputMessage数据
filter.setValue(infoData, forKey: "inputMessage")
//获得滤镜输出的图像
var outputImage = filter.outputImage ?? CIImage.init()
//图片小于(27,27),我们需要放大
outputImage = outputImage.transformed(by: CGAffineTransform.init(scaleX: 9, y: 9))
//创建彩色过滤器(彩色的用的不多)
let colorFilter = CIFilter(name: "CIFalseColor") ?? CIFilter.init()
//设置默认值
colorFilter.setDefaults()
//给私有属性赋值
colorFilter.setValue(outputImage, forKey: "inputImage")
//需要使用 CIColor
colorFilter.setValue(CIColor.init(color: backgroundColor), forKey: "inputColor0")
colorFilter.setValue(CIColor.init(color: mainColor), forKey: "inputColor1")
//设置输出
let colorCIImage = colorFilter.outputImage ?? CIImage.init()
let colorImage = UIImage(ciImage: colorCIImage)
return colorImage
}
}