3
\$\begingroup\$

Here is the code and it is really slow, like seconds slow to render about 25 labels.

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!
    }
}

So this takes way too much time. For 20 labels it takes seconds on the simulator.

\$\endgroup\$
  • \$\begingroup\$ Is the deBlur() method unrelated to the blur()? \$\endgroup\$ – kelin Dec 4 '17 at 20:52
  • \$\begingroup\$ First of all you can draw all labels in a single context, because context creation requires memory allocation which is quite slow. \$\endgroup\$ – kelin Dec 4 '17 at 20:54
  • \$\begingroup\$ Try using EAGLContext based on GPU Apple Doc \$\endgroup\$ – Coder ACJHP May 23 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.