views:

687

answers:

2

I have a UIView with a large number of CALayers (~1000), each of which has a small image as its contents. This UIView is a subview of a scrollview (actually it's a subview of another view which is a subview of the scrollview). This draws relatively quickly at first (couple seconds). However when I scroll in the scrollview the frame rate is very low.

Is it trying to redraw the contents of each CALayer every time I scroll? Is there a way to disable this? Is something else going on?

Note: I set clearsContextBeforeDrawing to NOo n my UIView and this helped somewhat but it's still much slow than I would expect

+1  A: 

Set the needsDisplayOnBoundsChange property on your layers to NO:

[layer setNeedsDisplayOnBoundsChange:NO];
rpetrich
Thanks but this doesn't do it. I'm not sure the bounds change on a subview of a scrollview when the scrollview is scrolled.
Daniel
+1  A: 

The layers should not be redrawn, but they will be composited using the GPU. Try using the Sampler and OpenGL ES instruments in Instruments to see if your bottleneck is in the CPU or GPU. You can log extra detail within the OpenGL ES instrument by tapping on the "i" next to the instrument name and selecting the extra parameters (Tiler Utilization, Renderer Utilization, etc.). If one of the OpenGL values is near 100%, then it's the compositing that's holding you back. However, if the sampler is showing a number of calls to -drawRect: or similar methods, then your layers are being redrawn (for some reason) and that could be the problem.

Additionally, you can use the Core Animation instrument to color non-opaque layers, which can lower your display framerate.

Brad Larson
Thanks Brad. I'll see if I can figure out if the composting step is the bottleneck. I'm guessing that it is. Do you have any thoughts on how to speed that stage up. I don't need it to recomposite when I scroll - is there a way to disable compositing and then manually recomposite when necessary (i.e. only when one of the layer contents actually changes)?
Daniel
To speed up compositing, use opaque layers whereever possible. Also, you might look at adding and removing sublayers as they come on the screen during your scrolling process. Are the images in the layers larger images that are scaled by a transform? If so, you might want to pre-scale the images and only something the exact size that you need. Transforms can also lead to a slight performance hit.
Brad Larson
These are all very good tips :)
rpetrich