Justin Lennox
Justin Lennox

Reputation: 147

Animation Terminating due to Memory even with ARC and imageFilePath

I'm doing a simple animation of png's after my app loads and displays its launch screen. The animation works in the simulator but not on the actual iPhone, where it terminates due to memory pressure (the launch screen does load first though). In the debug, the Memory increases exponentially to like 200MB until it crashes. The instruments show no leaks, All Heap and anonymous Vm 9.61 MB overall. The animation doesn't show at all on the actual iPhone.

I've already stopped using imageNamed to set the animation images and used imageFilePath as suggested by another help topic. It works and the images load on the simulator. I'm just not sure what else to do. Help would be very much appreciated.

In didFinishLaunchingWithOptions:

[self.window makeKeyAndVisible];
self.animationView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0, 320, 480)];
NSArray *animationArray = [[NSArray alloc] initWithObjects:
[UIImage imageFromMainBundleFile:@"/Splash_00000.png"],
[UIImage imageFromMainBundleFile:@"/Splash_00001.png"],

//There's about 150 of these so I'll omit the rest

                                      nil];

self.animationView.animationImages = animationArray;
self.animationView.animationDuration = 5.0f;
self.animationView.animationRepeatCount = 1;
[self.animationView startAnimating];
[self.window addSubview:self.animationView];
[self.window bringSubviewToFront:self.animationView];

In case it's needed, this is the method I'm using that I got from another thread:

+(UIImage*)imageFromMainBundleFile:(NSString*)aFileName
{
    NSString* bundlePath = [[NSBundle mainBundle] bundlePath];
    return [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@", bundlePath, aFileName]];
}

Upvotes: 0

Views: 193

Answers (1)

Dan Bennett
Dan Bennett

Reputation: 1450

Putting aside that splash screens aren't recommended, you're not leaking but you're running out of heap (which is why it works on the simulator).

Each of those images is owned by the array and won't be released by ARC until long after the animation has completed. Bare in mind that the PNGs, while compressed on disk, will be uncompressed in memory.

Solutions - there are a couple that spring to mind.

  1. Split the animations into a sequence of discrete phases and ensure that images are released (using @autoreleasepool) between each phase
  2. More realistically, render the animation as a movie and play that instead (far less likely to stutter between phases)

Upvotes: 2

Related Questions