Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2604280
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5920
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-11-15 17:38:01


http://www.cnblogs.com/smileEvday/archive/2013/03/28/Gif.html

IOS中如何解析并显示Gif

  聊天的时候我们经常用到很多搞笑的Gif表情,有时候千言万语真的比不上一个Gif来的实在。以前一直以为想Gif作为一种常用格式,各大平台应该都会支持,知道前两天项目中要加载一个Gif作为过渡动画,我查了半天帮助文档,发现IOS中居然没有现成的支持加载和播放Gif的类。

  于是上网简单的搜了一下,发现两个比较有有用的代码。网址分别如下:

      

  

  这两种方法都是采用ImageIO库,对Gif文件进行解析,然后自己做动画显示,至此显示Gif动画的思路就已经比较明确了。

  我们知道Gif是由一阵阵画面组成的,而且每一帧画面播放的时常可能会不相等,观察上面两个例子,发现他们都没有对Gif中每一帧的显示时常做处理,这样的结果就是整个Gif中每一帧画面都是以固定的速度向前播放,很显然这并不总会符合需求。

  于是自己写一个解析Gif的工具类,解决每一帧画面并遵循每一帧所对应的显示时间进行播放。

  程序的思路如下:

  1、首先使用ImageIO库中的CGImageSource家在Gif文件。

  2、通过CGImageSource获取到Gif文件中的总的帧数,以及每一帧的显示时间。

  3、通过CAKeyframeAnimation来完成Gif动画的播放。

  下面直接上我写的解析和播放Gif的工具类的代码:

复制代码
// // SvGifView.h // SvGifSample // // Created by maple on 3/28/13. // Copyright (c) 2013 smileEvday. All rights reserved. // // QQ: 1592232964 #import  @interface SvGifView : UIView /* * @brief desingated initializer */ - (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL; /* * @brief start Gif Animation */ - (void)startGif; /* * @brief stop Gif Animation */ - (void)stopGif; /* * @brief get frames image(CGImageRef) in Gif */ + (NSArray*)framesInGif:(NSURL*)fileURL; @end
复制代码
复制代码
// // SvGifView.m // SvGifSample // // Created by maple on 3/28/13. // Copyright (c) 2013 smileEvday. All rights reserved. // // QQ: 1592232964 #import "SvGifView.h" #import  #import  /* * @brief resolving gif information */ void getFrameInfo(CFURLRef url, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight)
{
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL(url, NULL); // get frame count size_t frameCount = CGImageSourceGetCount(gifSource); for (size_t i = 0; i < frameCount; ++i) { // get each frame CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        [frames addObject:(id)frame];
        CGImageRelease(frame); // get gif info with each frame NSDictionary *dict = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL);
        NSLog(@"kCGImagePropertyGIFDictionary %@", [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]); // get gif size if (gifWidth != NULL && gifHeight != NULL) { *gifWidth = [[dict valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue]; *gifHeight = [[dict valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];
        } // kCGImagePropertyGIFDictionary中kCGImagePropertyGIFDelayTime,kCGImagePropertyGIFUnclampedDelayTime值是一样的 NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];
        [delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]]; if (totalTime) { *totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue];
        }
    }
} @interface SvGifView() {
    NSMutableArray *_frames;
    NSMutableArray *_frameDelayTimes;
    
    CGFloat _totalTime; // seconds  CGFloat _width;
    CGFloat _height;
} @end @implementation SvGifView - (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
{
    self = [super initWithFrame:CGRectZero]; if (self) {
        
        _frames = [[NSMutableArray alloc] init];
        _frameDelayTimes = [[NSMutableArray alloc] init];
        
        _width = 0;
        _height = 0; if (fileURL) {
            getFrameInfo((CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height);
        }
        
        self.frame = CGRectMake(0, 0, _width, _height);
        self.center = center;
    } return self;
} + (NSArray*)framesInGif:(NSURL *)fileURL
{
    NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3];
    NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3];
    
    getFrameInfo((CFURLRef)fileURL, frames, delays, NULL, NULL, NULL); return frames;
} - (void)startGif
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];
    
    NSMutableArray *times = [NSMutableArray arrayWithCapacity:3];
    CGFloat currentTime = 0; int count = _frameDelayTimes.count; for (int i = 0; i < count; ++i) {
        [times addObject:[NSNumber numberWithFloat:(currentTime / _totalTime)]];
        currentTime += [[_frameDelayTimes objectAtIndex:i] floatValue];
    }
    [animation setKeyTimes:times];
    
    NSMutableArray *images = [NSMutableArray arrayWithCapacity:3]; for (int i = 0; i < count; ++i) {
        [images addObject:[_frames objectAtIndex:i]];
    }
    
    [animation setValues:images];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
    animation.duration = _totalTime;
    animation.delegate = self;
    animation.repeatCount = 5;
    
    [self.layer addAnimation:animation forKey:@"gifAnimation"];
} - (void)stopGif
{
    [self.layer removeAllAnimations];
} // remove contents when animation end - (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    self.layer.contents = nil;
} // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect
{ // Drawing code } @end
复制代码

  代码很短也比较容易,就不一一解释了。最开始的那个C函数主要就是用来解析Gif的,之所以用C函数是因为我要返回多个信息,而Objective-c只能返回一个参数,而且Objective-c和C语言可以很方便的混合编程。

   测试程序Github地址:

阅读(687) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~