如何将imageWithContentsOfFile用于animation中使用的图像数组?

这是我现在有:

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导致我一些内存问题,我想切换到imageWithContentsOfFile。

我可以使它与单个图像,但不是整个数组:

 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]; } 

任何帮助是极大的赞赏! 谢谢!

 for(int i = 1; i <= 21; i++) { [images addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"dance2_%03d", i] ofType:@"jpg"]]]; } 

你应该首先做的是用你的animation的图像创build一个像这样的数组:

 NSMutableArray* images = [[NSMutableArray alloc] initWithObjects: [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image1" ofType:@"jpg"]], [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"image2" ofType:@"jpg"]], nil]; 

那么你可以把它添加到一个UIImageView中来像这样做animation:

 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]; 

现在您可以使用这两个调用来启动和停止animation:

 [animationImagesView startAnimating]; //starts animation [animationImagesView stopAnimating]; //stops animation 

希望这可以帮助。 还记得发布和零你的数组和UIImageView完成后。