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

4

2 回答 2

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.

于 2013-09-14T06:17:30.177 回答
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.

于 2013-09-14T02:05:37.993 回答