美文网首页
Swift 模糊特效

Swift 模糊特效

作者: JaiUnChat | 来源:发表于2017-12-05 15:46 被阅读37次

如果需要在后台预先处理模糊的毛玻璃特效图片用于快速加载,那么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()就尽量用。

相关文章

网友评论

      本文标题:Swift 模糊特效

      本文链接:https://www.haomeiwen.com/subject/oldcixtx.html