1、从上下文获取CIImage
guard let originCGImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage else { return }
UIGraphicsEndImageContext()
let originCIImage = CIImage(cgImage: originCGImage)
2、给CIImage增加滤镜
guard let pinkMonochromeFilter = CIFilter(name: "CIColorMonochrome") else { return }
pinkMonochromeFilter.setDefaults()
pinkMonochromeFilter.setValue(originCIImage, forKey: kCIInputImageKey)
let pinkColor = CIColor(color: kltuiColor(withHex: 0xFF7538))
pinkMonochromeFilter.setValue(pinkColor, forKey: "inputColor")
pinkMonochromeFilter.setValue(1.0, forKey: "inputIntensity")
3、把加滤镜后的CIImage转成CGImage
let pinkCIImage = pinkMonochromeFilter.outputImage
let pinkCGImage = ciContext.createCGImage(pinkCIImage, from: pinkrect)
4、变换坐标轴,把CGImage绘制在CGContext上
context.textMatrix = .identity
context.translateBy(x: 0, y: totalHeight)
context.scaleBy(x: 1.0, y: -1.0)
context.draw(pinkCGImage!, in: pinkrect)
context.translateBy(x: 0, y: totalHeight)
context.scaleBy(x: 1.0, y: -1.0)