1

I'm using a UIImage animation and it is causing numerous memory leaks and crashes for different users using the application.

Below is my code. I am preloading the set of two animation in viewDidAppear

pointsView.image = [UIImage imageNamed:@"C72.png"];

    NSMutableArray *menuanimationImages = [[NSMutableArray alloc] initWithCapacity:21];
    NSString *imageName;

    for( int aniCount = 0; aniCount < 72; aniCount++ )
    {
        imageName = [NSString stringWithFormat:@"C%d.png", aniCount];
        [menuanimationImages addObject:[UIImage imageNamed:imageName]];
    }

    pointsView.animationImages = menuanimationImages;

    pointsView2.image = [UIImage imageNamed:@"I72.png"];

    NSMutableArray *menuanimationImagess = [[NSMutableArray alloc] initWithCapacity:21];
    NSString *imageNames;

    for( int aniCounts = 0; aniCounts < 72; aniCounts++ )
    {
        imageNames = [NSString stringWithFormat:@"I%d.png", aniCounts];
        [menuanimationImagess addObject:[UIImage imageNamed:imageNames]];
    }

    pointsView2.animationImages = menuanimationImagess;
}

I am then running the animation using

pointsView.animationDuration = 3.11;
pointsView.animationRepeatCount = 1;
[pointsView startAnimating];

Any suggestions?

2 Answers2

5

Please read my blog post about this subject: video-and-memory-usage-on-ios-devices. The root of the problem is that you simply cannot have this many images loaded into main memory at the same time. You need to simply not use the UIImageView.animationImages API, it is badly broken and lures developers into writing bad code that will crash when run on the device.

MoDJ
  • 4,309
  • 2
  • 30
  • 65
0

You are loading it looks like 72 png images into memory at once? And depending on the size of those images, you could probably be reaching the memory limits of some older devices causing them to give a memory warning and eventually crash. My suggestion is to not do a 72 image animation. You could try to compress each image which will lower their quality and memory size but loading 72 images to do an animation is just not good in the first place.

Kris Gellci
  • 9,539
  • 6
  • 40
  • 47
  • What if I load the images specifically when the button is pressed only. Is that still going to be an issue you think? –  May 15 '13 at 00:28
  • Any time that you load that many images into memory at once will be an issue. Keep in mind that mobile devices are more limited than desktop computers, especially the older ones. If you hook up an older device like an iPhone 3G or iPad to your mac, you will probably see some memory warning messages in the console while you fun through the for loop to allocate memory for the images. Eventually it will run out of memory and terminate the application. – Kris Gellci May 15 '13 at 00:34
  • Does releasing data from memory using [example release]; release it totally from memory? This is just a side question. Maybe if I free up space from other objects of the app it would work. –  May 15 '13 at 00:57
  • If you have ARC enabled then do example = nil; this tells the system that it is no longer in use and the memory can be recycled if needed, as long as there is nothing else pointing to that object. Else you can use release. – Kris Gellci May 15 '13 at 01:04
  • do you recommend me stopping the animations when the app receives the memory warning? –  May 15 '13 at 02:00
  • Yes and releasing all of the memory for all of the images. – Kris Gellci May 15 '13 at 14:51
  • when I stop the animations and release the memory when the warning occurs the app crashes. Any suggestions? –  May 15 '13 at 15:08
  • Yes, do not use the animation images API exposed in UIImageView. It is the root cause of your problem. You are simply using up all device memory. Just don't do that. – MoDJ Mar 23 '15 at 04:04