本文主要是介绍IOS Performance之CALayer shadow Sucks!,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
核心动画在设计的时候就考虑了性能。它首先是层级别的呈现,并且设计运行在小型的设备上(iphone和itouch),这些设备内存有限,并且cpu和gpu不如桌面电脑上的强大,核心动画是被设计的比较高效的,但是并不意味着你就可以在代码中随便用。
阴影也是代价很高的。因为他们属性部分透明的层,它需要大量的计算,来决定每个像素(因为每个像素都需要计算,直到有不透明的层遇到。如果阴影重叠的话,就增加了消耗。考虑限制只有最外层的阴影,并允许内层不产生任何阴影
QuartzCore layer.shadow吸性能。他们需要重新渲染,每次有新的变化,都会造成幻灯片,Lag,Lag。
http://stackoverflow.com/questions/10133109/fastest-way-to-do-shadows-on-ios/10133182#10133182
Adding a shadowPath should give you a huge performance boost. The following example assumes you only want the shadow on the sides of your view
CGPathRef path = [UIBezierPath bezierPathWithRect:view.bounds].CGPath;
[view.layer setShadowPath:path];
EDIT: On default a CALayer draws a shadow during animations, the following code allows you to cache the shadow as a bitmap and reuse it instead of redrawing it:
self.view.layer.shouldRasterize = YES;
// Don't forget the rasterization scale
// I spent days trying to figure out why retina display assets weren't working as expected
self.view.layer.rasterizationScale = [UIScreen mainScreen].scale;
还有一个答案是:
You can greatly improve the performance of a CALayer’s shadow by using its shadowPath
property—this allows it to draw the shadow without having to recalculate the alpha mask of the layer. For a rectangular view, you’d use it like this:
theView.layer.shadowPath = [UIBezierPath bezierPathWithRect:theView.bounds].CGPath;
or, if its corners are rounded,
theView.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:theView.bounds cornerRadius:theView.layer.cornerRadius].CGPath;
根据实际情况,提升性能!
这篇关于IOS Performance之CALayer shadow Sucks!的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!