How to apply CIFilter to UIView?
根据Apple docs, 的Apple docs, 过滤器属性,iOS 不支持。当我使用将CIFilter 应用于UIView 的应用程序之一时,即Splice,Funimate和artisto的视频编辑器Videoshow FX。这意味着我们可以将CIFilter 应用于UIView 。
我已经使用过
技术上准确的答案是
-
请注意,
CIImage 的原点位于左下角,而不是左上角。基本上,Y轴是翻转的。 -
如果您动态使用CoreImage滤镜,请学习使用
GLKView 进行渲染-它使用GPU,而UIImageView 使用CPU。 - 如果要测试过滤器,最好使用实际的设备。模拟器会给您非常差的性能。我已经看到一个简单的模糊需要将近一分钟的时间,而在设备上只需一秒钟的时间!
假设您要对CIPhotoEffectMono应用
-
将
UIView 转换为CIImage 。 -
应用过滤器,得到
CIImage 作为输出。 -
使用
CIContext 创建CGImage ,然后将其转换为UIImage 。
这里是
1 2 3 4 5 6 7 8 9 10 | extension UIView { public func createImage() -> UIImage { UIGraphicsBeginImageContextWithOptions( CGSize(width: self.frame.width, height: self.frame.height), true, 1) self.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image! } } |
将
1 | let ciInput = CIImage(image: myView.createImage) |
这是一个将应用过滤器并返回
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | func convertImageToBW(image:UIImage) -> UIImage { let filter = CIFilter(name:"CIPhotoEffectMono") // convert UIImage to CIImage and set as input let ciInput = CIImage(image: image) filter?.setValue(ciInput, forKey:"inputImage") // get output CIImage, render as CGImage first to retain proper UIImage scale let ciOutput = filter?.outputImage let ciContext = CIContext() let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!) return UIImage(cgImage: cgImage!) } |