How do I use imageWithContentsOfFile for an array of images used in animation?
This is what I have:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [NSString stringWithFormat:@"dance2_%03d.jpg", count];
UIImage *frame = [UIImage imageNamed:fileName];
[images addObject:frame];
}
UIImage imageNamed is causing some memory issues and I would like to switch to imageWithContentsOfFile.
I can get it to work with one image, but not the whole array:
NSMutableArray *images = [[NSMutableArray alloc] initWithCapacity:21];
for(int count = 1; count <= 21; count++)
{
NSString *fileName = [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/dance2_001.jpg"];
UIImage *frame = [UIImage imageWithContentsOfFile:fileName];
[images addObject:frame];
}
Any help is greatly appreciated! Thank!
+1
source to share
2 answers
what you should do first is create an array of images for your animation, for example something like this:
NSMutableArray* images = [[NSMutableArray alloc] initWithObjects:
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"jpg"]],
[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2" ofType:@"jpg"]],
nil];
then you can add it to the UIImageView to animate it like this:
UIImageView* animationImagesView = [[UIImageView alloc] initWithFrame:CGRectMake(posX, posY, frameWidth, frameHeight)];
animationImagesView.animationImages = images; //array of images to be animate
animationImagesView.animationDuration = 1.0; //duration of animation
animationImagesView.animationRepeatCount = 1; //number of time to repeat animation
[self.view addSubview:animationImagesView];
you can now start and stop animations with these two calls:
[animationImagesView startAnimating]; //starts animation
[animationImagesView stopAnimating]; //stops animation
hope this helps. also remember to free and play your array and UIImageView when done.
+1
source to share