GIF解析和生成
//gif的解析, 和生成
|
|
gif 解析方法
From: http://blog.csdn.net/qxuewei/article/details/50782855
原生方法:
1.UIWebView
特点:加载速度略长,性能更优,播放的gif动态图更加流畅。
-(void)showGifImageWithWebView{
NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"earthGif" ofType:@"gif"]];
UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(112, 302, 132, 102)];
imageWebView.userInteractionEnabled = NO;
[imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
[self.view addSubview:imageWebView];
}
2.UIImagView
加载的方式更加快速,性能不如UIWebView,优点:易于扩展
1)
增加一个UIImageView的类别(category),增加两个方法
UIImage+Tool
.h
#import <UIKit/UIKit.h>
@interface UIImageView (Tool)
/** 解析gif文件数据的方法 block中会将解析的数据传递出来 */
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray,NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths, NSArray<NSNumber *>* heights))dataBlock;
/** 为UIImageView添加一个设置gif图内容的方法: */
-(void)yh_setImage:(NSURL *)imageUrl;
@end
.m
#import "UIImageView+Tool.h"
#import <ImageIO/ImageIO.h>
@implementation UIImageView (Tool)
-(void)getGifImageWithUrk:(NSURL *)url returnData:(void(^)(NSArray<UIImage *> * imageArray, NSArray<NSNumber *>*timeArray,CGFloat totalTime, NSArray<NSNumber *>* widths,NSArray<NSNumber *>* heights))dataBlock{
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
size_t count = CGImageSourceGetCount(source);
float allTime=0;
NSMutableArray * imageArray = [[NSMutableArray alloc]init];
NSMutableArray * timeArray = [[NSMutableArray alloc]init];
NSMutableArray * widthArray = [[NSMutableArray alloc]init];
NSMutableArray * heightArray = [[NSMutableArray alloc]init];
for (size_t i=0; i<count; i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
[imageArray addObject:(__bridge UIImage *)(image)];
CGImageRelease(image);
NSDictionary * info = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, i, NULL);
CGFloat width = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelWidth] floatValue];
CGFloat height = [[info objectForKey:(__bridge NSString *)kCGImagePropertyPixelHeight] floatValue];
[widthArray addObject:[NSNumber numberWithFloat:width]];
[heightArray addObject:[NSNumber numberWithFloat:height]];
NSDictionary * timeDic = [info objectForKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];
CGFloat time = [[timeDic objectForKey:(__bridge NSString *)kCGImagePropertyGIFDelayTime]floatValue];
allTime+=time;
[timeArray addObject:[NSNumber numberWithFloat:time]];
}
CFRelease(source);
dataBlock(imageArray,timeArray,allTime,widthArray,heightArray);
}
-(void)yh_setImage:(NSURL *)imageUrl{
__weak id __self = self;
[self getGifImageWithUrk:imageUrl returnData:^(NSArray<UIImage *> *imageArray, NSArray<NSNumber *> *timeArray, CGFloat totalTime, NSArray<NSNumber *> *widths, NSArray<NSNumber *> *heights) {
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
NSMutableArray * times = [[NSMutableArray alloc]init];
float currentTime = 0;
for (int i=0; i<imageArray.count; i++) {
[times addObject:[NSNumber numberWithFloat:currentTime/totalTime]];
currentTime+=[timeArray[i] floatValue];
}
[animation setKeyTimes:times];
[animation setValues:imageArray];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
animation.repeatCount= MAXFLOAT;
animation.duration = totalTime;
[[(UIImageView *)__self layer]addAnimation:animation forKey:@"gifAnimation"];
}];
}
@end
在加载gif的地方使用
导入 UIImageView+Tool
-(void)showGifImageWithImageView{
UIImageView * imageView = [[UIImageView alloc]initWithFrame:CGRectMake(112, 342, 132, 102)];
NSURL * url = [[NSURL alloc]initFileURLWithPath:[[NSBundle mainBundle] pathForResource:@"earthGif.gif" ofType:nil]];
[imageView yh_setImage:url];
[self.view addSubview:imageView];
}
第三方:
1.YLGIFImage
github链接: [https://github.com/liyong03/YLGIFImage][1]
[1]: https://github.com/liyong03/YLGIFImage
#import “YLGIFImage.h”
#import "YLImageView.h"
-(void)showGifImageWithYLImageView{
YLImageView* imageView = [[YLImageView alloc] initWithFrame:CGRectMake(112, 342, 132, 102)];
CGFloat centerX = self.view.center.x;
[imageView setCenter:CGPointMake(centerX, 402)];
[self.view addSubview:imageView];
imageView.image = [YLGIFImage imageNamed:@"earthGif.gif"];
}
2.FLAnimatedImage
github链接:[https://github.com/Flipboard/FLAnimatedImage][2]
[2]: https://github.com/Flipboard/FLAnimatedImage
-(void)showGifImageWithFLAnimatedImage{
NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];
NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
imageView.animatedImage = image;
imageView.frame = CGRectMake(112, 342, 132, 102);
[self.view addSubview:imageView];
}
You need to set
install_url
to use ShareThis. Please set it in _config.yml
.