Tutorial :iPhone Dev: big png sequences cause crash?



Question:

I'm building an app which includes a number of image sequences (5 sequences with about 80 images each). It runs nicely in the iPhone simulator, but causes my iPhone to reboot when I test it. By the way, each png image is about 8k in size. Has anyone successfully built a similar app? Am I using too many resources for the iPhone to handle? Anyone?

UPDATE:
Thanks to all for you answers! I've modified my code to use [UIImage imageWithContentsOfFile:] instead of [UIImage imageNamed:] However I'm still unable to prevent the app from crashing my iPhone. (please note that my pngs are not that big about 400x400px / 8k)

Does anyone have any suggestions?

Here's my code:

    // code snippet:      myFrames = [[NSMutableArray alloc] initWithCapacity:maxFrames];              NSMutableString *curFrame;              num = 0;      // loop (maxframes = 80)              for(int f = 1; f < maxFrames+1; f++)              {                  curFrame = [NSMutableString stringWithString:tName];                    if(f < 10) [curFrame appendString:[NSString stringWithFormat:@"00%i",f]];                   else if(f>9 && f<100) [curFrame appendString:[NSString stringWithFormat:@"0%i",f]];                   else [curFrame appendString:[NSString stringWithFormat:@"%i",f]];                     UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:curFrame ofType:@"png"]];                    if(img) [myFrames addObject:img];                  [img release];              }  // animate the images!              self.animationImages = myFrames;              self.animationDuration = (maxFrames * .05); // Seconds              [self startAnimating];  


Solution:1

The best way to find out is to run the application under Instruments using Leaks or Object Alloc. If you see an upward trend that keeps rising, you might have a leak.

If you're using [UIImage imageNamed:], you should be aware that it pre-caches an optimized version which takes up more memory when compared with [UIImage imageWithContentsOfFile:]. Additionally, until iPhone 3.0, the cache created by [UIImage imageNamed:] doesn't get released when there's a memory warning.

The current-gen iPhone only has 128MB of ram, some of which is used by the OS itself. A 320x480 image fully uncompressed with an alpha channel can take 614k. If you have 400 unique images that are full screen, that's well over 128MB of ram, assuming it is loaded up and cached uncompressed.


Solution:2

The number one reason why an app would not crash on the simulator but on the phone would be memory

On the iphone simulator AFAIK the memory is not limited to 128Mb while on the iphone once it reaches 128Mb it restarts. So check your memory usage on the simulator. You have to change the way you are loading the images and or check for leaks. Also check if your getting low memory warnings by implementing the methods (I forgot what they are called :()


Solution:3

I've seen apps run in the simulator and not on the phone because of improper PNG formatting (even a single improperly formatted image can cause this crash). Check to make sure that the format of your images matches those of PNG files provided by apple in their example apps.

That being said 400 full screen images would easily cause it to run out of memory as in memory they will occupy far more than the 8kb. Not sure how big those images are, but if they're all in memory they will need to be very, very small on the iPhone.


Solution:4

The first answer to your question states that while your PNGs may take up only 8K on disk, that is the compressed on-disk form. When it is loaded into memory, it is decompressed and is much larger than 8K. At 32-bits per pixel, a 400x400 image will be 640K.

Even without the alpha channel, you're looking at 480K. 480K x 80 frames, that is 38.4MB, which is definitely creeping into using more memory than the iphone has available to give your app at once. Here is an article about some of the troubles with obtaining a substantial about of memory from the iPhone OS.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »