CALayer performance vs. UIImageView performance

2019-03-28 13:16发布

I'm noticing that some of the things that I do with images in my app, I could be doing with CALayer properties instead (i.e. borders, shadows, gradients, and so on). So I'm wondering, in general, is it a better idea to use CALayers and such, or just do all that stuff in photoshop and use UIImage(View)s for whatever I need? Scrolling performance has been kinda slow with drop shadows and stuff, so I'm basically trying to find every little optimization that I can.

Background information: I'm basically just scrolling through a bunch of images, vertically. Somewhat like the photos app. This is all from 3.2 onwards, so I have access to things like the shadow and shouldRasterize properties in CALayer.

1条回答
成全新的幸福
2楼-- · 2019-03-28 13:34

A key thing to know about CALayer shadows is that they are, by default, rendered from the layer's composited alpha channel; that can be really slow, particularly when the layer is rotating or changing size. You should look into the shadowPath property: if you give Core Animation a simple shape, such as a rectangle (UIBezierPath +bezierPathWithRect: with the layer's bounds will give you [via its CGPath property] a CGPathRef you can use there), you'll see much better performance.

shouldRasterize is also definitely useful, particularly with views that have many subviews that don't change often; make sure, though, that you set the layer's rasterizationScale to [UIScreen mainScreen].scale, or you'll get some nasty pixelation on high-resolution screens like the iPhone 4's.

查看更多
登录 后发表回答