gpt4 book ai didi

swift - 如何修复 "Optimization Opportunities"

转载 作者:行者123 更新时间:2023-12-03 16:15:24 25 4
gpt4 key购买 nike

我正在使用 xcode 12。
我写了扩展 UI View 如下:

@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
}
}
一切正常,但是当我调试 View 时,我看到了一些像这样的紫色警告。

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


有人可以向我解释这一点并帮助我摆脱它吗?
Here are the purple warnings

最佳答案

设置 shadowPath
一种解决方案是通过设置 shadowPath 来“引导”阴影渲染。明确满足您的需求,例如:

yourViewWithShadow.layer.shadowPath = UIBezierPath(rect: yourViewWithShadow.bounds).cgPath
确保您在正确的时间设置框架!
缓存光栅化
另一种解决方案是缓存光栅化:
yourViewWithShadow.layer.shouldRasterize = true
yourViewWithShadow.layer.rasterizationScale = UIScreen.main.scale
希望这可以帮助您消除昂贵的计算。

关于swift - 如何修复 "Optimization Opportunities",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64277067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com