GifView.h/***  调用结束就开始播放动画,如果需要用户指定何时播放的话,只需要把timer的开始放到合适的位置。通过对CFDictonaryRaf 也就是gifProperties的改变,我们还可以控制动画是否循环播放以及循环多少次停止。通过对index的改变也可以控制动画从某帧开始播放。同理,同时改变index和count的话,也可以控制从某帧到某帧的播放。注意:- (void)stopGif;之后才可以退出这个类。否则timer不会关闭,产生内存泄露。*/#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>@interface GifView : UIView {CGImageSourceRef gif; // 保存gif动画NSDictionary *gifProperties;  // 保存gif动画属性size_t index;// gif动画播放开始的帧序号size_t count;// gif动画的总帧数NSTimer *timer;// 播放gif动画所使用的timer
}- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;
- (id)initWithFrame:(CGRect)frame data:(NSData *)_data;
- (void)stopGif;GifView.m
#import "GifView.h"
#import <QuartzCore/QuartzCore.h>@implementation GifView- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath{self = [super initWithFrame:frame];if (self) {gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]forKey:(NSString *)kCGImagePropertyGIFDictionary];gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);count =CGImageSourceGetCount(gif);timer = [NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(play) userInfo:nil repeats:YES];[timer fire];}return self;
}- (id)initWithFrame:(CGRect)frame data:(NSData *)_data{self = [super initWithFrame:frame];if (self) {gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]forKey:(NSString *)kCGImagePropertyGIFDictionary];//        gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:_filePath], (CFDictionaryRef)gifProperties);gif = CGImageSourceCreateWithData((CFDataRef)_data, (CFDictionaryRef)gifProperties);count =CGImageSourceGetCount(gif);timer = [NSTimer scheduledTimerWithTimeInterval:0.12 target:self selector:@selector(play) userInfo:nil repeats:YES];[timer fire];}return self;
}-(void)play
{index ++;index = index%count;CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);self.layer.contents = (__bridge id)ref;CFRelease(ref);
}
-(void)removeFromSuperview
{NSLog(@"removeFromSuperview");[timer invalidate];timer = nil;[super removeFromSuperview];
}
- (void)dealloc {NSLog(@"dealloc");CFRelease(gif);
}
- (void)stopGif
{[timer invalidate];timer = nil;
}

加载Gif的三种方式:(从网络或者本地)

- (NSData *)loadDataForIndex:(NSInteger)index {NSData *data = nil;if (index == 0) {//网络data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://s14.sinaimg.cn/mw690/005APVsyzy6MFOsVFfv5d&690"]];}else {//本地data = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"run" ofType:@"gif"]];}return data;
}

1.GifView

    //第三方GifView(实现gif动画播放是通过将动画文件读取到CGImageSourceRef,然后用NSTimer来播放的。)//- (id)initWithFrame:(CGRect)frame filePath:(NSString *)_filePath;GifView *dataView = [[GifView alloc] initWithFrame:CGRectMake(0, 0, 100, 100) data:data];[self.view addSubview:dataView];
//    [dataView stopGif];

2.webView(不会出现内存问题)

    //webViewUIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 120, 100, 100)];webView.backgroundColor = [UIColor redColor];webView.scalesPageToFit = YES;[webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];[self.view addSubview:webView];

3.帧动画

- (void)runGIFForImage {UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 240, 100, 100)];NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"],[UIImage imageNamed:@"2"],[UIImage imageNamed:@"3"],[UIImage imageNamed:@"4"],[UIImage imageNamed:@"5"],[UIImage imageNamed:@"6"],[UIImage imageNamed:@"7"],[UIImage imageNamed:@"8"],[UIImage imageNamed:@"9"],[UIImage imageNamed:@"10"],[UIImage imageNamed:@"11"],[UIImage imageNamed:@"12"],[UIImage imageNamed:@"13"],[UIImage imageNamed:@"14"],[UIImage imageNamed:@"15"],[UIImage imageNamed:@"16"],[UIImage imageNamed:@"17"],[UIImage imageNamed:@"18"],[UIImage imageNamed:@"19"],[UIImage imageNamed:@"20"],[UIImage imageNamed:@"21"],[UIImage imageNamed:@"22"],nil];gifImageView.animationImages = gifArray; //动画图片数组gifImageView.animationDuration = 5; //执行一次完整动画所需的时长gifImageView.animationRepeatCount = 999;  //动画重复次数[gifImageView startAnimating];[self.view addSubview:gifImageView];
}
 

转载于:https://www.cnblogs.com/shifu/p/4807740.html

加载gif动画的三种方式相关推荐

  1. ios网络学习------4 UIWebView的加载本地数据的三种方式

    ios网络学习------4 UIWebView的加载本地数据的三种方式 分类: IOS2014-06-27 12:56 959人阅读 评论(0) 收藏 举报 UIWebView是IOS内置的浏览器, ...

  2. @Import-给容器中加载bean的第三种方式

    给容器中注册组件: 1)包扫描+组件标注注解(@Controller/@Service/@Repository/@Component) 2)@Bean[导入的第三方包里面的组件] 3)@Import[ ...

  3. [ROS2] map_server加载地图文件的三种模式

    map的数据类型 map话题的类型是nav_msgs::msg::OccupancyGrid.使用下面的命令可以查询该类型的数据结构. ros2 interface show nav_msgs/msg ...

  4. c语言加载本地图片,Unity加载本地图片的2种方式

    1. 使用 WWW 加载,详细查看 unity3d 官方文档. 2. 使用 System.IO 加载,lua 代码如下: local File = luanet.import_type("S ...

  5. java配置文件实现方式_java相关:详解Spring加载Properties配置文件的四种方式

    java相关:详解Spring加载Properties配置文件的四种方式 发布于 2020-4-29| 复制链接 摘记: 一.通过 context:property-placeholder 标签实现配 ...

  6. spring加载ApplicationContext.xml的四种方式

    spring加载ApplicationContext.xml的四种方式 spring 中加载xml配置文件的方式,好像有4种, xml是最常见的spring 应用系统配置源.Spring中的几种容器都 ...

  7. Android加载GIF图片的两种方式

    飞哥语录:得到一件东西最好的方式是让自己配得上它. 方式一:使用第三开源框架直接在布局文件中加载gif 1.在工程的build.gradle中添加如下 buildscript {repositorie ...

  8. Spring加载properties文件的两种方式

    2019独角兽企业重金招聘Python工程师标准>>> 在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取pro ...

  9. Python加载csv文件的两种方式

    本文主要讲解下Python加载csv文件的两种方式,如果知道如何处理的就不必往下看了! 下面来简单介绍下. 实例中的数据集是kaggle的Digit Recognizer的train.csv文件,数据 ...

最新文章

  1. [思考]-32位的应用程序为什么不能跑64位的应用程序
  2. 定时修改列表 服务器版,Unity定时回调(服务端不依赖Update)
  3. 如何查看linux系统的体系结构
  4. weekendplan
  5. 【2018.4.7】模拟赛之四-ssl2385 猜数【水题】
  6. delphi7 获取dll的类_跟我学Java内存管理----JMM精华终章(类加载器)
  7. GCC:使用图对比编码的图神经网络预训练模型 KDD2020
  8. html5拖放原理,HTML5 拖放实现
  9. 如何在python中识别未知的日期时区时区
  10. 雷林鹏分享:MySQL DELETE 语句
  11. 全自动降噪插件-Acon Digital Extract:Dialogue 1.1.2 WiN-MAC
  12. 如何判断电路反馈类型
  13. 计算机第四章文字处理软件应用课后答案,计算机应用基础第四章文字处理软件.doc...
  14. 根治偏头痛及各种头痛病症
  15. LVGL 获取光标坐标位置
  16. 关于“前台根据后台值,操作字段、显示或select选中状态”的几种做法
  17. 关于51单片机按键防抖的一个思考
  18. tomcat启动错误Error deploying web application directory
  19. X265码率控制——ABR算法基本原理 源码解读
  20. 企业应用架构模式-30天阅读计划

热门文章

  1. 老男孩Day15作业:商城列表页面(静态)
  2. asp mysql 查询记录数_ASP如何查询ACCESS数据库中上一周的所有记录
  3. ubuntu中使用默认apt 安装的java配置JAVA_HOME
  4. 如何用xib自定义View
  5. 清华大学微电子所所长魏少军谈芯片行业发展
  6. python中randn出错_randn和normal之间的Python区别 - python
  7. 金丘区块链联盟链云平台海星链升级至3.0,以ECOChain(生态链)亮相2018广州黄埔区块链成果发布会...
  8. HFSS 初学者入门(一)
  9. 巨头征战新零售,实体小玩家如何求得生机?
  10. (二十三)树莓派3B+ 驱动蜂鸣器播放《茉莉花》