1.使用UIWebView播放
#pragma clang diagnostic ignored "-Wnonnull"
NSString *path = [[NSBundle mainBundle] pathForResource:@"<#gifName#>" ofType:@"gif"];
NSData *gifData = [NSData dataWithContentsOfFile:path];
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>)];
webView.scalesPageToFit = YES;
[webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
[self.view addSubview:webView];
2.將GIF圖片分解成多張PNG圖片,使用UIImageView播放。
需要導(dǎo)入#import <ImageIO/ImageIO.h>
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:@"<#gifName#>" withExtension:@"gif"]; //加載GIF圖片
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef) fileUrl, NULL); //將GIF圖片轉(zhuǎn)換成對(duì)應(yīng)的圖片源
size_t frameCout = CGImageSourceGetCount(gifSource); //獲取其中圖片源個(gè)數(shù)泽腮,即由多少幀圖片組成
NSMutableArray *frames = [[NSMutableArray alloc] init]; //定義數(shù)組存儲(chǔ)拆分出來的圖片
for (size_t i = 0; i < frameCout; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); //從GIF圖片中取出源圖片
UIImage *imageName = [UIImage imageWithCGImage:imageRef]; //將圖片源轉(zhuǎn)換成UIimageView能使用的圖片源
[frames addObject:imageName]; //將圖片加入數(shù)組中
CGImageRelease(imageRef);
}
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(<#x#>, <#y#>, <#w#>, <#h#>)];
gifImageView.animationImages = frames; //將圖片數(shù)組加入U(xiǎn)IImageView動(dòng)畫數(shù)組中
gifImageView.animationDuration = 0.15; //每次動(dòng)畫時(shí)長
[gifImageView startAnimating]; //開啟動(dòng)畫系吩,此處沒有調(diào)用播放次數(shù)接口端朵,UIImageView默認(rèn)播放次數(shù)為無限次液肌,故這里不做處理
[self.view addSubview:gifImageView];
3.如何播放NSData數(shù)據(jù)的GIF
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {
if (!data) {
return nil;
}
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source);
UIImage *animatedImage;
if (count <= 1) {
animatedImage = [[UIImage alloc] initWithData:data];
}
else {
NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = 0.0f;
for (size_t i = 0; i < count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
duration += [self sd_frameDurationAtIndex:i source:source];
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
CGImageRelease(image);
}
if (!duration) {
duration = (1.0f / 10.0f) * count;
}
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
}
CFRelease(source);
return animatedImage;
}
+ (float)sd_frameDurationAtIndex:(NSUInteger)index source:(CGImageSourceRef)source {
float frameDuration = 0.1f;
CFDictionaryRef cfFrameProperties = CGImageSourceCopyPropertiesAtIndex(source, index, nil);
NSDictionary *frameProperties = (__bridge NSDictionary *)cfFrameProperties;
NSDictionary *gifProperties = frameProperties[(NSString *)kCGImagePropertyGIFDictionary];
NSNumber *delayTimeUnclampedProp = gifProperties[(NSString *)kCGImagePropertyGIFUnclampedDelayTime];
if (delayTimeUnclampedProp) {
frameDuration = [delayTimeUnclampedProp floatValue];
}
else {
NSNumber *delayTimeProp = gifProperties[(NSString *)kCGImagePropertyGIFDelayTime];
if (delayTimeProp) {
frameDuration = [delayTimeProp floatValue];
}
}
// Many annoying ads specify a 0 duration to make an image flash as quickly as possible.
// We follow Firefox's behavior and use a duration of 100 ms for any frames that specify
// a duration of <= 10 ms. See <rdar://problem/7689300> and <http://webkit.org/b/36082>
// for more information.
if (frameDuration < 0.011f) {
frameDuration = 0.100f;
}
CFRelease(cfFrameProperties);
return frameDuration;
}
至于哪種方式性能好呢?
使用UIWebView性能會(huì)好點(diǎn)焰络,UIImageView播放是通過定時(shí)器來控制圖片模擬動(dòng)畫的戴甩,它們控制的楨速是固定的。如果設(shè)置的模擬楨速跟gif本身的楨速相近的話倒沒什么闪彼,如果楨速相差過大就很耗性能甜孤。