正常情況下,如果我們用UIImageView來直接加載本地GIF圖片,運(yùn)行顯示圖片并不是動(dòng)態(tài)的,但是如果通過網(wǎng)絡(luò)獲取的GIF圖片,即可正常顯示動(dòng)態(tài)效果;
在這里我要講述的是如何通過UIImageView來順利加載本地GIF圖片的方法:
NSURL *gifImageUrl = [[NSBundle mainBundle] URLForResource:@"GIF圖片名稱" withExtension:@"gif"];
//獲取Gif圖的原數(shù)據(jù)
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)gifImageUrl, NULL);
//獲取Gif圖有多少幀
size_t gifcount = CGImageSourceGetCount(gifSource);
NSMutableArray* voiceImages = [NSMutableArray array];
for (NSInteger i = 0; i < gifcount; i++) {
//由數(shù)據(jù)源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值越小,運(yùn)動(dòng)速度越快
_iconImageView.animationDuration = 0.7;
//animationRepeatCount代表循環(huán)次數(shù),默認(rèn)是0,表示無限循環(huán)
_iconImageView.animationRepeatCount = 0;
//這句話一定要記得加,啟動(dòng)動(dòng)畫;頁面消失時(shí)別忘了stopAnimating
[_iconImageView startAnimating];