有什么方法可以加速这个UILabel模糊代码?

时间:2017-10-29 15:15:26

标签: ios swift uilabel gaussianblur

这是代码,它非常慢,例如渲染大约25个标签的速度很慢。

extension UILabel{
    func deBlur(){
        for subview in self.subviews {
            if (subview.tag == 99999) {
                subview.removeFromSuperview()
            }
        }
    }

    func blur(){
        let  blurRadius:CGFloat = 5.1

        UIGraphicsBeginImageContext(bounds.size)
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let blurFilter = CIFilter(name: "CIGaussianBlur")
        blurFilter?.setDefaults()
        let imageToBlur = CIImage(cgImage: (image?.cgImage)!)
        blurFilter?.setValue(imageToBlur, forKey: kCIInputImageKey)
        blurFilter?.setValue(blurRadius, forKey: "inputRadius")
        let outputImage: CIImage? = blurFilter?.outputImage
        let context = CIContext(options: nil)
        let cgimg = context.createCGImage(outputImage!, from: (outputImage?.extent)!)
        layer.contents = cgimg!
    }
}

任何形象/ UIGraphics大师都知道为什么这么懒散?

更新:这行代码是罪魁祸首。但是,还需要创建模糊效果。

        let cgimg = UILabel.context.createCGImage(outputImage!, from: (outputImage?.extent)!)

0 个答案:

没有答案