Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "Optimization Opportunities"

I'm using xcode 12. I wrote extension UI View as below:

@IBInspectable
public var shadowRadius: CGFloat {
    get {
        return layer.shadowRadius
    }
    set {
        layer.shadowRadius = newValue
    }
}

@IBInspectable
public var shadowOpacity: Float {
    get {
        return layer.shadowOpacity
    }
    set {
         layer.shadowOpacity = newValue
    }
}

@IBInspectable
public var shadowOffset: CGSize {
    get {
        layer.shadowOffset
    }
    set {
         layer.shadowOffset = newValue
    }
}

@IBInspectable
public var shadowColor: UIColor {
    get {
        return UIColor(cgColor: layer.shadowColor ?? UIColor.clear.cgColor)
    }
    
    set {
        layer.shadowColor = newValue.cgColor
    }
}

Things works fine but when I debug view, I saw some purple warnings like this.

x-xcode-debug-views://7f8fd2258020?DBGViewDebuggerLaunchSessionParameter=7f8fd2258020: runtime: Optimization Opportunities: The layer is using dynamic shadows which are expensive to render. If possible try setting shadowPath, or pre-rendering the shadow into an image and putting it under the layer.

Can someone explain this to me and help me to get rid of it??

Here are the purple warnings

like image 220
Thao Tran Avatar asked Oct 09 '20 09:10

Thao Tran


1 Answers

Setting shadowPath

One solution is to "guide" the shadow rendering by setting the shadowPath explicitly to your needs, eg.:

yourViewWithShadow.layer.shadowPath = UIBezierPath(rect: yourViewWithShadow.bounds).cgPath

Make sure you're setting the frame at the right time!

Cache rasterization

Another solution is to cache the rasterization:

yourViewWithShadow.layer.shouldRasterize = true
yourViewWithShadow.layer.rasterizationScale = UIScreen.main.scale

Hope this helps you to eliminate expensive calculations.

like image 151
choofie Avatar answered Nov 04 '22 01:11

choofie