//

//  loadGifView.h

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface loadGifView : UIView

/**

*  @brief 初始化

*

*  @return

*/

- (instancetype)initWithFrame:(CGRect)frame localFileURL:(NSURL *)fileURL;

/**

*  @brief 开始gif动画

*

*  @return

*/

- (void)startGifAnimation;

/**

*  @brief 停止gif动画

*

*  @return

*/

- (void)stopGifAnimation;

/**

*  @brief 得到gif中所有的图片的frame

*

*  @return

*/

+ (NSArray *)getFramesFromGif:(NSURL *)fileURL;

@end

//

//  loadGifView.m

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//  播放gif

#import "loadGifView.h"

#import <ImageIO/ImageIO.h>

#import <QuartzCore/CoreAnimation.h>

@interface loadGifView ()

@property (nonatomic, strong) NSMutableArray * frames;

@property (nonatomic, strong) NSMutableArray * frameDelayTimes;

@property (nonatomic, assign) CGFloat totalTime;

@property (nonatomic, assign) CGFloat width;

@property (nonatomic, assign) CGFloat height;

@end

@implementation loadGifView

/**

* @brief  得到gif图片的信息

*

* @param

* @return

*/

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:(__bridge id)frame];

CGImageRelease(frame);

// get gif info with each frame

NSDictionary *dict = (NSDictionary*)CFBridgingRelease(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];

}

CFRelease(CFBridgingRetain(dict));

}

if (gifSource) {

CFRelease(gifSource);

}

}

- (instancetype)initWithFrame:(CGRect)frame localFileURL:(NSURL *)fileURL

{

self = [super initWithFrame:frame];

if (self) {

_frames = [[NSMutableArray alloc] init];

_frameDelayTimes = [[NSMutableArray alloc] init];

_width = 0;

_height = 0;

if (fileURL) {

getFrameInfo((__bridge CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height);

}

}

return self;

}

+ (NSArray *)getFramesFromGif:(NSURL *)fileURL

{

NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3];

NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3];

getFrameInfo((__bridge CFURLRef)fileURL, frames, delays, NULL, NULL, NULL);

return frames;

}

- (void)startGifAnimation

{

CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"contents"];

NSMutableArray *times = [NSMutableArray arrayWithCapacity:3];

CGFloat currentTime = 0;

int count = (int)_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 = MAXFLOAT;

[self.layer addAnimation:animation forKey:@"gifAnimation"];

}

- (void)stopGifAnimation

{

[self.layer removeAllAnimations];

}

@end

//

//  PlayGifViewController.h

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import <UIKit/UIKit.h>

@interface PlayGifViewController : UIViewController

@end

//

//  PlayGifViewController.m

//  PlayGif

//

//  Created by 寒竹子 on 15/4/27.

//  Copyright (c) 2015年 寒竹子. All rights reserved.

//

#import "PlayGifViewController.h"

#import "loadGifView.h"

@interface PlayGifViewController ()

@property (nonatomic, strong) NSArray * imageArr;

@end

@implementation PlayGifViewController

- (void)viewDidLoad {

[super viewDidLoad];

self.view.backgroundColor = [UIColor blackColor];

// 播放gif动画

NSString * path = [[NSBundle mainBundle] pathForResource:@"2.gif" ofType:nil];

loadGifView * gifView = [[loadGifView alloc] initWithFrame:CGRectMake(([UIScreen mainScreen].bounds.size.width - 200) / 2.0f, 100, 200, 300) localFileURL:[NSURL fileURLWithPath:path]];

[gifView startGifAnimation];

[self.view addSubview:gifView];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

@end

转载于:https://www.cnblogs.com/hanzhuzi/p/4461877.html

iOS 播放Gif动画相关推荐

  1. iOS 播放gif动态图的方式探讨

    原文链接:http://my.oschina.net/u/2340880/blog/608560 摘要 iOS中没有现成的接口来展示gif动态图,但可以通过其他的方式来处理gif图的展示. iOS中播 ...

  2. 一步一步教你实现iOS音频频谱动画(一)

    如果你想先看看最终效果再决定看不看文章 -> bilibili 示例代码下载 第二篇:一步一步教你实现iOS音频频谱动画(二) 基于篇幅考虑,本次教程分为两篇文章,本篇文章主要讲述音频播放和频谱 ...

  3. iOS中 Animation 动画大全 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 1.iOS中我们能看到的控件都是UIView的子类,比如UIButt ...

  4. 一步一步教你实现iOS音频频谱动画(二)

    如果你想先看看最终效果再决定看不看文章 -> bilibili 示例代码下载 第一篇:一步一步教你实现iOS音频频谱动画(一) 本文是系列文章中的第二篇,上篇讲述了音频播放和频谱数据计算,本篇讲 ...

  5. iOS学习——核心动画之Layer基础

    iOS学习--核心动画之Layer基础 1.CALayer是什么? CALayer我们又称它叫做层.在每个UIView内部都有一个layer这样一个属性,UIView之所以能够显示,就是因为它里面有这 ...

  6. android中播放gif动画之二

    2019独角兽企业重金招聘Python工程师标准>>> 在上一篇,是使用代码通过构造方法传入要播放的gif动画的id进行获取的.本文进一步改造,让GifView和ImageView一 ...

  7. Unity3d(U3D) Windows/Android/IOS 播放rtmp/rtsp方案

    如果基于Unity3d完全重新开发一个播放器,代价大,周期长,不适合快速出产品,最好的方式就是集成现有Native平台上成熟稳定播放器. 集成Navtive播放器可行性分析: 安卓: Unity3d可 ...

  8. Android高级控件(二)——SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现...

    Android高级控件(二)--SurfaceView实现GIF动画架包,播放GIF动画,自己实现功能的初体现 写这个的原因呢,也是因为项目中用到了gif动画,虽然网上有很多的架包可以实现,不过我们还 ...

  9. WPF中播放Flash动画

    WPF本身不直接支持播放Flash动画,需要做点"设计",下面是示例(嵌入WPF窗口播放). 1.首先在VS2010中建立项目.打开VS2010后选择"文件"- ...

最新文章

  1. strapi 开源api 内容管理平台试用
  2. css垂直居中问题~
  3. 问题清空easyui required=true的提示信息所在位置不对。乱跑的解决办法
  4. 单机redis 主从实例
  5. Qt中Ui名字空间以及setupUi函数的原理和实现
  6. java remote desktop_Remote Desktop
  7. 【知识强化】第一章 操作系统概述 1.1 操作系统的基本概念
  8. luogu P1762 偶数
  9. 阻止中文输入法输入拼音的时候触发input事件
  10. windows安装MySQL数据库【附安装文档和安装包】
  11. Git常用命令及场景
  12. 数据中心效率:40%的改进是通过最佳实践方案
  13. [JAVA毕业设计]高铁在线购票系统源码获取和系统演示
  14. windows server 2008 r2 设置显示文件后缀名
  15. C语言入门基础_验证哥德巴赫猜想
  16. IEEE Transactions on Industrial Informatics(TII)投稿须知
  17. 生活无大事,生活无小事,需用心经营才行
  18. Form derives from Form
  19. 金融知识小科普 - 北向资金
  20. 牛顿迭代法算法的实现

热门文章

  1. 面试题10-二进制中1的个数
  2. Android开发网
  3. 热点热词 新闻热点 最新新闻数据API接口-天狗热点热词开放平台
  4. Linux下MySQL基础使用命令
  5. 资源监视工具 Glances
  6. Windows上的svn仓库迁移(visualSVN)
  7. gvim设置字体和隐藏菜单栏工具栏
  8. CodeForces Round #191 (327C) - Magic Five 等比数列求和的快速幂取模
  9. android的内存泄露有几种,Android中几种有可能会导致内存泄露的情况
  10. C#调用VB进行简繁转换