Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CALayer optimizations?

Tags:

ios

calayer

I'm adding several CALayers as sublayers of the layer of a UIView. The contents of each layer is a different image downloaded from a server. Each layer is animated from offscreen to a randomly generated position. The image data is downloaded asynchronously. Each image is approx 300x300 or smaller.

As a result of the random placement, the layers overlap and some are obscured by the layers above them. This is all good.

I'm removing layers as they become completely obscured from view using the suggestion in the answer to this question The calculations to determine coverage occur on a separate thread.

I have a UIPanGestureRecognizer that allows the user to drag the layers around on the screen.

I'm encountering a performance problem when the number of layers added approaches 25-30 and gets progressively worse. The animations become choppy and often are completely absent (the newly added layers simply appear in their final position). And the pan gestures are either ignored or result in choppy repositioning of the selected layer.

I'm supposing that I'm killing the GPU with all of the layers overlapping and another layer animating above?

Any suggestions on how to improve performance?

Best practices for dealing with large numbers of layers?

Is it better to have the layer begin animated in a separate view.layer than the previously added layers?

Thanks!

like image 985
TomH Avatar asked Jan 31 '11 04:01

TomH


2 Answers

Couple of quick things to check.

Run the Core Animation Instrument and look for opacity. Just setting the layer's opaque flag to YES is not enough, if the underlying image has an alpha component the layer will take that into consideration.

If the data you are getting from your sever has alpha then you should redraw with Quartz and save the file locally in the new format that does not include alpha.

Make doubly sure that you are not putting 1 mega pixel images into 100x100 tiles. Also Core Animation Instrument, switching on 'Color Misaligned Images' and look for yellow.

30 to 50 layers should be no problem.

like image 171
Bill Dudney Avatar answered Oct 05 '22 09:10

Bill Dudney


If all the layers don't fit in the GPU's memory or portion of memory, things will slow down a lot. Loading and unloading GPU memory is reported to be quite slow compared to compositing layers in GPU memory. You'll have to experiment with this memory limit, as older devices have less GPU memory available than more recent devices.

like image 23
hotpaw2 Avatar answered Oct 05 '22 08:10

hotpaw2