1

After failing to get a live image filter on a Metal view to perform better, I was curious if my UI had some kind of impact. I have an extension on UIButton that adds a blur and shadow to them. If I comment out the stuff inside the functions, and run it, the GPU works twice as hard and the battery impact races beyond high in the debug navigator.

extension UIButton
{
    func addCircularBlur(radius:CGFloat)
    {

        let blur = UIVisualEffectView(effect: UIBlurEffect(style: .regular))
        blur.frame = self.bounds
        blur.layer.cornerRadius = radius * self.bounds.size.width
        blur.clipsToBounds = true
        blur.isUserInteractionEnabled = false
        blur.autoresizingMask = [.flexibleRightMargin, .flexibleLeftMargin, .flexibleBottomMargin, .flexibleTopMargin, .flexibleWidth, .flexibleHeight]

        self.insertSubview(blur, at: 0)
        if let imageView = self.imageView{
            self.bringSubview(toFront: imageView)
        }
    }

    func addShadow()
    {
        self.imageView?.layer.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 1.0).cgColor
        self.imageView?.layer.shadowOffset = CGSize(width:0.0, height:0.0)
        self.imageView?.layer.shadowOpacity = 0.1
        self.imageView?.layer.shadowRadius = 2.0
        self.imageView?.layer.masksToBounds = false
        self.imageView?.layer.cornerRadius = 4.0
    }

}

Here's how I'm calling these functions. This example uses a button that is from Interface Builder, but it can be added to a button programmatically:

@IBOutlet weak var takePhotoBtn: UIButton!

// inside viewDidLoad:
takePhotoBtn.addCircularBlur(radius:0.5)
takePhotoBtn.addShadow()

What about this is making my device perform better when the blur and shadows are ON?

Chewie The Chorkie
  • 4,896
  • 9
  • 46
  • 90
  • I don't believe they make your app performance better. They are just there for convenience. – Rakesha Shastri Sep 14 '18 at 18:10
  • But they ARE making my app perform better. Try it yourself with a full screen camera view using Metal in iOS. My guess is one of the properties being set in the blur effect that offload work. – Chewie The Chorkie Sep 14 '18 at 18:21
  • _whoops_ i was misleaded by the title. :x My bad. – Rakesha Shastri Sep 14 '18 at 18:26
  • The title is accurate. Using the UIButton effect functions are making my app perform better. – Chewie The Chorkie Sep 14 '18 at 18:35
  • Let me rephrase... i mislead myself by not reading the title properly and jumping to conclusions. _peace_ – Rakesha Shastri Sep 14 '18 at 18:36
  • Got it, thanks for clearing that up. – Chewie The Chorkie Sep 14 '18 at 18:49
  • How are you using `addCircularBlur` and `addShadow`? Please show how they are called. Also, have you tried commenting out (or not calling) only one function? I've seen some related questions from you and this isn't tagged Metal, so I'm assuming nothing is being run on the GPU - after all `UIKit` (for instance `UIButton` should normally use the CPU. Finally, if Metal *is* involved, have you tried directing it to use the CPU? –  Sep 14 '18 at 20:52
  • dfd - Updated. Yes, I tested commenting out only the addCircularBlur func and then tested commenting out addShadow. Both have a positive impact by adding these. What is "it" when you ask if I tried directing it to use the CPU? Do you mean the buttons? I'm unsure how I'd specify it one way or another. My only guess is UIVisualEffectView is somehow using Metal and offloads unnecessary work for the entire UIButton to the GPU, but that doesn't explain the addShadow effect. – Chewie The Chorkie Sep 14 '18 at 21:02
  • Try this: comment out everything **except** bringing the image view forward and turning off its `masksToBounds`. – Ken Thomases Sep 14 '18 at 21:15
  • Ken - That still makes it race into high battery usage. I wonder now if Xcode is just giving a faulty report of battery usage until I comment out these button effects. Is it possible? – Chewie The Chorkie Sep 17 '18 at 14:51
  • I confirmed that the energy impact reading does not have to do with a Metal project. It can be a normal session with the camera and filter onto a CIImage on a regular UIImageView. But still, weather the UIButton effect functions are commented out or not show a huge difference on the battery impact. – Chewie The Chorkie Sep 17 '18 at 15:38

0 Answers0