CALayer 性能与 UIImageView 性能

人气:549 发布:2022-10-16 标签: iphone uikit uiimageview calayer

问题描述

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.

解决方案

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.

681