/**count:图片数量 name:图片名称*/ - (void)runAnimationWithCount:(int)count name:(NSString *)name { if(self.tom.isAnimating)return;//如果动画正在执行就不重复执行 NSMutableArray *images=[NSMutableArray array];//创建一个可变数组 for (int i=0; i<90; i++) { NSString *filename=[NSString stringWithFormat:@"%@_%02d.jpg",name,i]; //1.有缓存(传入文件名) //UIImage *image=[UIImage imageNamed:filename]; //2.imageWithContentsOfFile没有缓存(传入文件的全路径) NSBundle *bundle=[NSBundle mainBundle]; NSString *path=[bundle pathForResource:filename ofType:nil]; UIImage *image=[UIImage imageWithContentsOfFile:path]; [images addObject:image];//把图片添加到数组中 } //添加图片到数组中 self.tom.animationImages=images; //设置播放次数(1次) self.tom.animationRepeatCount=1; //设置播放时间 self.tom.animationDuration=images.count*0.1; [self.tom startAnimating]; //2.方法一:用一个定时器来清除最后一个图片所占内存 CGFloat delay=self.tom.animationDuration+1.0;//动画执行完成后一秒清除缓存 [self performSelector:@selector(clearCache) withObject:nil afterDelay:delay]; //2.方法二 //[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay]; } - (void)clearCache { self.tom.animationImages=nil; }