IOS performance calayer shadow sucks!

Source: Internet
Author: User

Core animations are designed with performance considerations. First, it is hierarchical presentation and is designed to run on small devices (iPhone and itouch). These devices have limited memory and the CPU and GPU are not as powerful as those on the desktop, the core animation is designed to be more efficient, but it does not mean you can use it in your code.

Shadow is also costly. Because their attributes are partial transparent layers, it requires a lot of computing to determine each pixel (because each pixel needs to be calculated until there is an opaque layer encountered. If the shadows overlap, the consumption will increase. Consider limiting only the shadow of the outermost layer and allowing the inner layer not to produce any shadow.

Quartzcore layer. Shadow performance. They need to re-render, and every new change will causeSlide, lag, lag.


Http://stackoverflow.com/questions/10133109/fastest-way-to-do-shadows-on-ios/10133182#10133182

Adding a shadowpath shoshould 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
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;

Another answer is:

You can greatly improve the performance of a calayer's shadow by using itsshadowPathProperty-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;

Improve performance based on actual conditions!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.