IOS Performance之CALayer shadow Sucks!

來源:互聯網
上載者:User

核心動畫在設計的時候就考慮了效能。它首先是層層級的呈現,並且設計運行在小型的裝置上(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 expectedself.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;

根據實際情況,提升效能!

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.