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播放。
需要导入#import <ImageIO/ImageIO.h>
NSURL *fileUrl = [[
NSBundle mainBundle] URLForResource:
@"<#gifName#>" withExtension:
@"gif"];
CGImageSourceRef gifSource =
CGImageSourceCreateWithURL((
CFURLRef) fileUrl,
NULL);
size_t frameCout =
CGImageSourceGetCount(gifSource);
NSMutableArray *frames = [[
NSMutableArray alloc] init];
for (size_t i =
0; i < frameCout; i++) {
CGImageRef imageRef =
CGImageSourceCreateImageAtIndex(gifSource, i,
NULL);
UIImage *imageName = [
UIImage imageWithCGImage:imageRef];
[frames addObject:imageName];
CGImageRelease(imageRef);
}
UIImageView *gifImageView = [[
UIImageView alloc] initWithFrame:
CGRectMake(<
#x#>, <#y#>, <#w#>, <#h#>)];
gifImageView.animationImages = frames;
gifImageView.animationDuration =
0.15;
[gifImageView startAnimating];
[
self.view addSubview:gifImageView];
3.如何播放NSData数据的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];
}
}
if (frameDuration <
0.011f) {
frameDuration =
0.100f;
}
CFRelease(cfFrameProperties);
return frameDuration;
}
至于哪种方式性能好呢?
使用UIWebView性能会好点,UIImageView播放是通过定时器来控制图片模拟动画的,它们控制的桢速是固定的。如果设置的模拟桢速跟gif本身的桢速相近的话倒没什么,如果桢速相差过大就很耗性能。