正常情况下,如果我们用UIImageView来直接加载本地GIF图片,运行显示图片并不是动态的,但是如果通过网络获取的GIF图片,即可正常显示动态效果;
在这里我要讲述的是如何通过UIImageView来顺利加载本地GIF图片的方法:
NSURL *gifImageUrl = [[NSBundle mainBundle] URLForResource:@"GIF图片名称" withExtension:@"gif"];
//获取Gif图的原数据
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)gifImageUrl, NULL);
//获取Gif图有多少帧
size_t gifcount = CGImageSourceGetCount(gifSource);
NSMutableArray* voiceImages = [NSMutableArray array];
for (NSInteger i = 0; i < gifcount; i++) {
//由数据源gifSource生成一张CGImageRef类型的图片
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
UIImage *image = [UIImage imageWithCGImage:imageRef];
[voiceImages addObject:image];
CGImageRelease(imageRef);
}
_iconImageView.image = voiceImages[0];
_iconImageView.animationImages = voiceImages;
//animationDuration值越小,运动速度越快
_iconImageView.animationDuration = 0.7;
//animationRepeatCount代表循环次数,默认是0,表示无限循环
_iconImageView.animationRepeatCount = 0;
//这句话一定要记得加,启动动画;页面消失时别忘了stopAnimating
[_iconImageView startAnimating];