1

I have a series of .png images loaded into an animation, which works fine, but when I switch to another view, the memory that the animation uses is not freed up. Which takes up more and more memory every time you return to that view until eventually the app quits due to too much memory pressure.

Is there anything I can put in view diddisappear or another method to free this memory from the animation when the view is changed?

Also this only seems to happen in with my 4S running iOs 7. On my 4S with 6.1.2 it runs smoothly.

NSArray *animationArray = [NSArray arrayWithObjects:
                               [UIImage imageNamed:@"0001.png"],
                               [UIImage imageNamed:@"0002.png"],
                               [UIImage imageNamed:@"0003.png"],

...

                               nil];
    self->tapanimation1.animationImages =animationArray;
    self->tapanimation1.animationDuration = .5;
    self->tapanimation1.animationRepeatCount = 1;
    [self->tapanimation1 startAnimating];

enter image description here

aasatt
  • 600
  • 3
  • 16
  • ok I edited it to show the code for the animation – aasatt Sep 14 '13 at 06:44
  • `imageNamed` caches the images (though they're released under memory pressure, so if you're crashing due to a lack of memory, then the cache is not the problem). But `imageWithContentsOfFile` can take the cache issue out of the loop, so you can confirm whether that's the issue or not. But I suspect something else must be going on. – Rob Sep 14 '13 at 06:59
  • The app worked fine without the animations. I'm just in the process of updating it for iOS 7 and wanted to add some more flare to it but these animations are getting in the way of how its functioning so I don't think it's something else. – aasatt Sep 14 '13 at 08:11

2 Answers2

4

How you alloc image in animation --- there are two ways of intializing UIImage

First is -->

[UIImage imageNamed:@"kshitij.png"];

There is issue with this method it doesnot dealloc memory , even on release .

Second is -->

If image is in your app Bundle,then always use this

 NSString *filePath = [[NSBundle mainBundle]pathForResource:@"kshitij" ofType:@"png"];
 UIImage *image = [[UIImage alloc]initWithContentsOfFile:filePath];

Now use this image and make it release too , it will surely will save your some MB. and you can also use ARC for memory saving.

Try this code --

NSSMutableArray *imageNameArray = [[NSSMutableArray
alloc]initWithObjects:@"0001",@"0002",nil];

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

for(int i=0;i<imageNameArray.count;i++)
{
   NSString *filePath = [[NSBundle mainBundle]pathForResource:[imageNameArray
   objectAtIndex:i] ofType:@"png"]; 
   UIImage *image = [[UIImage alloc]initWithContentsOfFile:filePath];

   [imageArray addObject:image];
}

self->tapanimation1.animationImages = imageArray;
self->tapanimation1.animationDuration = .5;
self->tapanimation1.animationRepeatCount = 1;
[self->tapanimation1 startAnimating];

Now when animation does stop just release array. If you are using ARC then make it nil.

KlimczakM
  • 12,576
  • 11
  • 64
  • 83
kshitij godara
  • 1,523
  • 18
  • 30
  • I don't fully understand what is going on in the second example. I am using ARC and it gives me ARC Semantic Issues when I try to use it. – aasatt Sep 14 '13 at 08:15
  • okay I must have no idea what I'm doing but I can't figure out how to use that code properly. See my edited post for a screenshot. – aasatt Sep 14 '13 at 19:49
  • @Rob Got it. Thank you! I'm working on this now but I think this is what I need. – aasatt Sep 14 '13 at 20:31
0

How are you navigating to your new view controller, and how are you getting back? Are you using an unwind segue? Are you presenting the new view controller as a modal and then dismissing it?

Or are you using a segue to link to the second VC, and then another segue to link back? If you are using a second (non-unwind) segue to link back, then that's your problem. That would cause you to create a new instance of your first view controller each time.

As for releasing memory, you could certainly set your view's animationImages array to inil in viewDidDisappear, and then re-load the images in viewWillAppear, but it doesn't make sense that you memory footprint would go up with each round trip to the second view controller. That indicates a problem.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Your probably right because I am not using an unwind segue; I'm just linking back. The memory usage jumps about 30mb when it first loads then every time it loads after it jumps between 1.5-3mb. I just want it to return back where it normally is, when it's on the Home VC, when I go back after I dismiss the view. – aasatt Sep 14 '13 at 06:06
  • Just implemented the unwind and it is not making a difference in the memory usage. – aasatt Sep 14 '13 at 08:09
  • The root cause of your problem is that you cannot allocate a huge array of UIImage objects. Your code need to only load 1 image at a time, otherwise older devices will crash due to memory usage. More info about this problem can be found here http://stackoverflow.com/questions/8112698/how-to-do-animations-using-images-efficiently-in-ios – MoDJ Nov 11 '14 at 05:37