1 png图片建议放在Images.xcassets文件中
2 jpg图片放在Images.xcassets文件中不支持,拖到Supporting Files文件中
3 加载图片
//1.加载多有动画图片
if(self.imageView.isAnimation) retrun;
NSMutableArray *images=[NSMutableArray array];
fot(int i=0;i<80;i++){
//计算文件名
NSString *filename=[NSString stringWithFormat:@"drink_%02d.jpg",i]; // drink_%02d.jpg 意为: %2 为占位 中间0 不够的用0补齐 drink_07.jpg
//加载图片
UIImage *image=[UIImage imageNamed:filename];
//添加图片到数组
[images addObjest:image];
}
self. imageView.animationImages=images;
//2.设置播放次数
self.imageView.animationRepeatCount=1;
//3.设置播放时间
self.imageView.animationDuration=images.count*0.05 images.count*0.05目的:保证匀速
[self.imageView startAnimating];
CGFloat delay=self.imageView.animationDuration+1.0;
[self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
在执行某一动画时不允许做其他添加一行代码 if(self.imageView.isAnimation) retrun;
加载imageView的两种方式
imageNamed 有缓存,传文件名
UIImage *image=[UIImage imageNamed:filename]; 适合加载小型图片,会有缓存,加载大型图片不建议
imageWithContentsOffFile 没有缓存,传文件的全路径
NSBundle *bundle=[NSBundle mainBundle];
NSString *path=[bundle pathForResource:filename ofType:nil];
UIImage *image=[UIImage imageWithContentsOffFile:path];
程序动画执行后会释放内存
CGFloat delay=self.imageView.animationDuration+1.0;
[self performSelector:@selector(clearCache) withObject:nil afterDelay:delay];
-(void)clearCache{
self.imageView.animationImages=nil;
}
上述代码又可简化为 [self performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:delay]; //给setAnimationImages:传一个参数为空