IOS 加载GIF图片
2016-11-22 00:00
253 查看
摘要: IOS加载GIF,UIlmageView 显示gif
需要显示GIF图片
![](https://static.oschina.net/uploads/space/2016/1122/154141_948t_1398907.gif)
1,需要添加一个UIImageView的分类
#import "UIImageView+ASGif.h"
#import <ImageIO/ImageIO.h>
#if __has_feature(objc_arc)
#define toCF (__bridge CFTypeRef)
#define ARCCompatibleAutorelease(object) object
#else
#define toCF (CFTypeRef)
#define ARCCompatibleAutorelease(object) [object autorelease]
#endif
@implementation UIImageView(ASGif)
- (void)animatedGIFImageSource:(CGImageSourceRef) source
andDuration:(NSTimeInterval) duration {
if (!source) return;
size_t count = CGImageSourceGetCount(source);
NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
for (size_t i = 0; i < count; ++i) {
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, i, NULL);
if (!cgImage)
return;
[images addObject:[UIImage imageWithCGImage:cgImage]];
CGImageRelease(cgImage);
}
[self setAnimationImages:images];
[self setAnimationDuration:duration];
[self startAnimating];
}
- (NSTimeInterval)durationForGifData:(NSData *)data {
char graphicControlExtensionStartBytes[] = {0x21,0xF9,0x04};
double duration=0;
NSRange dataSearchLeftRange = NSMakeRange(0, data.length);
while(YES){
NSRange frameDescriptorRange = [data rangeOfData:[NSData dataWithBytes:graphicControlExtensionStartBytes
length:3]
options:NSDataSearchBackwards
range:dataSearchLeftRange];
if(frameDescriptorRange.location!=NSNotFound){
NSData *durationData = [data subdataWithRange:NSMakeRange(frameDescriptorRange.location+4, 2)];
unsigned char buffer[2];
[durationData getBytes:buffer];
double delay = (buffer[0] | buffer[1] << 8);
duration += delay;
dataSearchLeftRange = NSMakeRange(0, frameDescriptorRange.location);
}else{
break;
}
}
return duration/100;
}
- (void)showGifImageWithData:(NSData *)data {
NSTimeInterval duration = [self durationForGifData:data];
CGImageSourceRef source = CGImageSourceCreateWithData(toCF data, NULL);
[self animatedGIFImageSource:source andDuration:duration];
CFRelease(source);
}
- (void)showGifImageWithURL:(NSURL *)url {
NSData *data = [NSData dataWithContentsOfURL:url];
[self showGifImageWithData:data];
}
@end
2,导入图片的分类 UIImageView(ASGif)
NSString *extensionName = newsData.iconUrl.pathExtension;
if ([extensionName.lowercaseString isEqualToString:@"gif"]) {
[self.icon showGifImageWithURL:[NSURL URLWithString:newsData.iconUrl]];
} else {
[self.icon setImageWithURL:[NSURL URLWithString:newsData.iconUrl] placeholderImage:[UIImage imageNamed:@"pic_cut_dis"]];
}
需要显示GIF图片
![](https://static.oschina.net/uploads/space/2016/1122/154141_948t_1398907.gif)
1,需要添加一个UIImageView的分类
#import "UIImageView+ASGif.h"
#import <ImageIO/ImageIO.h>
#if __has_feature(objc_arc)
#define toCF (__bridge CFTypeRef)
#define ARCCompatibleAutorelease(object) object
#else
#define toCF (CFTypeRef)
#define ARCCompatibleAutorelease(object) [object autorelease]
#endif
@implementation UIImageView(ASGif)
- (void)animatedGIFImageSource:(CGImageSourceRef) source
andDuration:(NSTimeInterval) duration {
if (!source) return;
size_t count = CGImageSourceGetCount(source);
NSMutableArray *images = [NSMutableArray arrayWithCapacity:count];
for (size_t i = 0; i < count; ++i) {
CGImageRef cgImage = CGImageSourceCreateImageAtIndex(source, i, NULL);
if (!cgImage)
return;
[images addObject:[UIImage imageWithCGImage:cgImage]];
CGImageRelease(cgImage);
}
[self setAnimationImages:images];
[self setAnimationDuration:duration];
[self startAnimating];
}
- (NSTimeInterval)durationForGifData:(NSData *)data {
char graphicControlExtensionStartBytes[] = {0x21,0xF9,0x04};
double duration=0;
NSRange dataSearchLeftRange = NSMakeRange(0, data.length);
while(YES){
NSRange frameDescriptorRange = [data rangeOfData:[NSData dataWithBytes:graphicControlExtensionStartBytes
length:3]
options:NSDataSearchBackwards
range:dataSearchLeftRange];
if(frameDescriptorRange.location!=NSNotFound){
NSData *durationData = [data subdataWithRange:NSMakeRange(frameDescriptorRange.location+4, 2)];
unsigned char buffer[2];
[durationData getBytes:buffer];
double delay = (buffer[0] | buffer[1] << 8);
duration += delay;
dataSearchLeftRange = NSMakeRange(0, frameDescriptorRange.location);
}else{
break;
}
}
return duration/100;
}
- (void)showGifImageWithData:(NSData *)data {
NSTimeInterval duration = [self durationForGifData:data];
CGImageSourceRef source = CGImageSourceCreateWithData(toCF data, NULL);
[self animatedGIFImageSource:source andDuration:duration];
CFRelease(source);
}
- (void)showGifImageWithURL:(NSURL *)url {
NSData *data = [NSData dataWithContentsOfURL:url];
[self showGifImageWithData:data];
}
@end
2,导入图片的分类 UIImageView(ASGif)
NSString *extensionName = newsData.iconUrl.pathExtension;
if ([extensionName.lowercaseString isEqualToString:@"gif"]) {
[self.icon showGifImageWithURL:[NSURL URLWithString:newsData.iconUrl]];
} else {
[self.icon setImageWithURL:[NSURL URLWithString:newsData.iconUrl] placeholderImage:[UIImage imageNamed:@"pic_cut_dis"]];
}
相关文章推荐
- iOS-使用系统类库加载gif格式图片
- iOS加载GIF动态图片
- FLAnimatedImage -ios gif图片加载框架介绍
- iOS 加载Gif图片
- iOS之加载Gif图片的方法
- IOS学习之路二十四(UIImageView 加载gif图片)
- iOS 加载GIF图片
- iOS 两种加载GIF图片区别
- IOS加载Gif图片的N种方式 By-H罗
- FLAnimatedImage -ios gif图片加载框架介绍
- iOS中GIF图片的加载
- iOS之加载Gif图片
- IOS学习之路二十四(UIImageView 加载gif图片)
- iOS知识 ----加载显示gif图片
- iOS开发之初:根据一张gif图,返回其中每一帧的图片数组(ImageView加载gif))
- Xamarin.ios 使用webview加载gif图片
- iOS加载Gif图片的N种方式 By-H罗
- iOS 使用SDWebImage加载gif链接图片时内存过大问题
- iOS加载Gif图片的N种方式
- ios加载gif图片