Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance-wise: A lot of small PNGs or one large PNG?

Developing a simple game for the iPhone, what gives a better performance?

  1. Using a lot of small (10x10 to 30x30 pixels) PNGs for my UIViews' backgrounds.
  2. Using one large PNG and clipping to the bounds of my UIViews.

My thought is that the first technique requires less memory per individual UIView, but complicates how the iPhone handles the large amount of images, as it tries to combine the images into a larger texture or tries to switch between all the small textures a lot.

The second technique, on the other hand, gives the iPhone the opportunity to handle just one large PNG, but unnessicarily increases the image weight every UIView has to carry.

  • Am I right about the iPhone's attempts, handling the images the way I described it?
  • So, what is the way to go?

Seeing the answers thus far, there is still doubt. There seems to be a trade-off with two parameters: Complexity and CPU-intensive coding. What would be my tipping point for deciding what technique to use?

like image 895
Kriem Avatar asked May 04 '09 12:05

Kriem


1 Answers

If you end up referring back to the same CGImageRef (for example by sharing a UIImage *), the image won't be loaded multiple times by the different views. This is the technique used by the videowall Core Animation demo at the WWDC 07 keynote. That's OSX code, but UIViews are very similar to CALayers.

The way Core Graphics handles images (from my observation anyway) is heavily tweaked for just-in-time loading and aggressive releasing when memory is tight.

Using a large image you could end up loading the image at draw time if the memory for the decoded image that CGImageRef points to has been reclaimed by the system.

What makes a difference is not how many images you have, but how often the UIKit traverses your code.

Both UIViews and Core Animation CALayers will only repaint if you ask them to (-setNeedsDisplay), and the bottleneck usually is your code plus transferring the rendered content into a texture for the graphics chip.

So my advice is to think your UIView layout in a way that allows portions that change together to be updated all at the same time, which turn into a single texture upload.

like image 117
duncanwilcox Avatar answered Oct 23 '22 07:10

duncanwilcox