Swift 模糊特效
2017-12-05 本文已影响37人
JaiUnChat
如果需要在后台预先处理模糊的毛玻璃特效图片用于快速加载,那么UIVisualEffectView()便不好用了。�
CoreImage
的方法如下
func addBlurLayer(){
let layer = CALayer()
layer.frame = CGRect(x: 0, y: 0, width: 150, height: 120)
layer.backgroundColor = UIColor.blue.cgColor
let ciInput = CIImage(image: UIImage(named: "D1")!)
let filter = CIFilter(name: "CIGaussianBlur", withInputParameters: ["inputImage": ciInput, "inputRadius": 5])
let ciOutput = filter?.outputImage
let ciContext = CIContext()
let cgImage = ciContext.createCGImage(ciOutput!, from: (ciOutput?.extent)!)
layer.contents = cgImage
displayView.layer.addSublayer(layer)
}
以一张400*500的图片为例。
在6s真机上的处理速度在0.02-0.04s左右,模拟器在5s左右
而UIVisualEffctecView()
处理1/7左右的面积耗时 在0.002s左右。不过模糊半径也不一样,总得来说么,能用UIVisualEffctecView()
就尽量用。