1

My application is in ARC.
I am using image Animation in imageView but after some period of time my application crashed with low memory error.

I am not getting any error message in Log but in Profile tool i am getting "Low Memory" message in allocation Part.

One code of my Animation is

ImageView= [[UIImageView alloc] init];
[self.view addSubview:ImageView];
ImageView.frame=CGRectMake(0, 0, 480, 342);
ImageView.animationImages = [NSArray arrayWithObjects:
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image3" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image4" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image5" ofType:@"png"]],
                                  [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image6" ofType:@"png"]],
                                  nil];
[ImageView setAnimationRepeatCount:1];
ImageView.animationDuration =1.0;
[ImageView startAnimating];

If i comment this animation code then application is running correctly but i need more than 5 this type of animation in one view.

Any Link, tutorial, any Idea will be great help...

Sam
  • 431
  • 7
  • 23

2 Answers2

0

Why don't you try with a sprite? I've tried to animate with CALater (Core Animation Layer) and it works like a charm. I animate the 'contentsRect'. Short code example (CALayer subclass):

self.contents = [UIImage imageNamed:@"sprite-large.png"].CGImage;

NSMutableArray *frames = [NSMutableArray array]; // array of CGRect's
CAKeyframeAnimation *anim = [CAKeyframeAnimation animationWithKeyPath:@"contentsRect"];
anim.values = frames;
[self addAnimation:anim forKey:nil];

I've posted some code to GitHub a few weeks ago: https://github.com/Gerharbo/sprite-animation-layer/

It provides an example sprite (not perfect, but it works) and some example code.

Have fun animating!

Gerhard

Gerharbo
  • 286
  • 1
  • 6
0

Gerharbo's method is basically a texture atlas but with one much bigger image. What I think you will find is that this kind of approach does not scale well when you try to use many images (for a long animation). You are already seeing crashes caused by running out of memory, so it would be a good idea to completely avoid UIImageView.animationImages. This blog post video-and-memory-usage-on-ios-devices describes why you cannot allocate memory to hold a bunch of images in main memory, which is exactly what you code above is trying to do. If you look around on SO you will find many other developers have ran into this same issue.

Community
  • 1
  • 1
MoDJ
  • 4,309
  • 2
  • 30
  • 65