iOS-Gif图片展示N种方式(原生+第三方)
原生方法:
1.UIWebView
特点:载入速度略长,性能更优。播放的gif动态图更加流畅。
//动态展示GIF图片-WebView
-(void)showGifImageWithWebView{
//读取gif图片数据
NSData *gifData = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"earthGif" ofType:@"gif"]];
//UIWebView生成
UIWebView *imageWebView = [[UIWebView alloc] initWithFrame:CGRectMake(112, 302, 132, 102)];
//用户不可交互
imageWebView.userInteractionEnabled = NO;
//载入gif数据
[imageWebView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
//视图加入此gif控件
[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
//
// UIImageView+Tool.m
// OneHelper
//
// Created by qiuxuewei on 16/3/2.
// Copyright © 2016年 邱学伟. All rights reserved.
//
#import "UIImageView+Tool.h"
//要引入ImageIO库
#import <ImageIO/ImageIO.h>
@implementation 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{
//通过文件的url来将gif文件读取为图片数据引用
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
//获取gif文件里图片的个数
size_t count = CGImageSourceGetCount(source);
//定义一个变量记录gif播放一轮的时间
float allTime=0;
//存放全部图片
NSMutableArray * imageArray = [[NSMutableArray alloc]init];
//存放每一帧播放的时间
NSMutableArray * timeArray = [[NSMutableArray alloc]init];
//存放每张图片的宽度 (一般在一个gif文件里,全部图片尺寸都会一样)
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]];
}
dataBlock(imageArray,timeArray,allTime,widthArray,heightArray);
}
//为UIImageView加入一个设置gif图内容的方法:
-(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;
//Layer层加入
[[(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
#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
-(void)showGifImageWithFLAnimatedImage{
//GIF 转 NSData
//Gif 路径
NSString *pathForFile = [[NSBundle mainBundle] pathForResource: @"earthGif" ofType:@"gif"];
//转成NSData
NSData *dataOfGif = [NSData dataWithContentsOfFile: pathForFile];
//初始化FLAnimatedImage对象
FLAnimatedImage *image = [FLAnimatedImage animatedImageWithGIFData:dataOfGif];
//初始化FLAnimatedImageView对象
FLAnimatedImageView *imageView = [[FLAnimatedImageView alloc] init];
//设置GIF图片
imageView.animatedImage = image;
imageView.frame = CGRectMake(112, 342, 132, 102);
[self.view addSubview:imageView];
}
iOS-Gif图片展示N种方式(原生+第三方)的更多相关文章
- iOS 收起键盘的几种方式
iOS 收起键盘的几种方式 1.一般的view上收起键盘 // 手势 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ ...
- iOS拨打电话的三种方式
iOS拨打电话的三种方式 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 1 2 var string = "tel:" + "1 ...
- iOS 登陆的实现四种方式
iOS 登陆的实现四种方式 一. 网页加载: http://www.cnblogs.com/tekkaman/archive/2013/02/21/2920218.ht ml [iOS登陆的实现] A ...
- Java基础知识强化之IO流笔记44:IO流练习之 复制图片的 4 种方式案例
1. 复制图片的 4 种方式案例: 分析: 复制数据,如果我们知道用记事本打开并能够读懂,就用字符流,否则用字节流. 通过该原理,我们知道我们应该采用字节流. 而字节流有4种方式,所以做这个题目我们有 ...
- 【Xamarin 挖墙脚系列:IOS 开发界面的3种方式】
原文:[Xamarin 挖墙脚系列:IOS 开发界面的3种方式] xcode6进行三种基本的界面布局的方法,分别是手写UI,xib和storyboard.手写UI是最早进行UI界面布局的方法,优点是灵 ...
- IOS文件操作的两种方式:NSFileManager操作和流操作
1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...
- iOS tabbar 图片,最佳大小方式
iOS tabbar 图片,最佳大小方式 文档大小 30 *30 retaina 60 *60 最佳大小 48 *32 参考:http://stackoverflow.com/questions/15 ...
- jQuery 实现图片放大两种方式
jQuery 实现图片放大两种方式 一.利用css样式表实现,多用于后台显示 1.这种比较简单,利用dom元素的hover实现样式切换 <style> img{ cursor: point ...
- [Android开发学iOS系列] iOS写UI的几种方式
[Android开发学iOS系列] iOS写UI的几种方式 作为一个现代化的平台, iOS的发展也经历了好几个时代. 本文讲讲iOS写UI的几种主要方式和各自的特点. iOS写UI的方式 在iOS中写 ...
随机推荐
- 22、Flask实战第22天:Flask信号
Flask中的信号使用的是一个第三方插件blinker.通过pip list看一下是否安装,如果没有,则使用如下命令安装 pip install blinker 自定义信号 自定义信号分为3步: ①定 ...
- 前端设计师必须知道的10个重要的CSS技巧
对于一个初入门的前端设计师,在设计修改网站前端的时候,我们需要编写一些CSS.JS的内容达到界面效果.今天分享10个对于前端设计师来说重要的CSS技巧,这也是我在给许多客户做网站的过程当中总结出来的. ...
- RobotFramework自动化测试框架系统关键字之断言
一.基础 RobotFramework带有丰富的系统关键,使用时无需导入,直接使用,为写自动化用例带来了极大的方便:不能停留在知道或者是会得程度,只有熟练使用各关键字,才能提升自动化用例的写作效率.下 ...
- 洛谷 - Sdchr 的邀请赛 T4 信息传递
(乱搞艹爆正解系列) 对不起,由于博主太弱了,并不会正解的多项式exp(甚至多项式exp我都不会2333). 只能来说一说我是怎么乱搞的啦QWQ 首先这个题最关键的性质是: 一个在原置换 g 中长度为 ...
- 某考试 T2 orzcyr
非常nice的一道行列式的题目. 考虑如果没有路径不相交这个限制的话,那么这个题就是一个行列式:设 a[i][j] 为从编号第i小的源点到编号第j小的汇点的路径条数,那么矩阵a[][]的行列式就是的答 ...
- [CF98E]Help Shrek and Donkey
题意:A和B两个卡牌大师玩游戏,A有$n$张牌,B有$m$张牌,桌上有$1$张牌,这$n+m+1$张牌互不相同且A和B都知道这些牌里有什么牌(但他们互相不知道对方有什么牌,两个人也都不知道桌上的那张牌 ...
- 【莫队算法】【权值分块】poj2104 K-th Number / poj2761 Feed the dogs
先用莫队算法保证在询问之间转移的复杂度,每次转移都需要进行O(sqrt(m))次插入和删除,权值分块的插入/删除是O(1)的. 然后询问的时候用权值分块查询区间k小值,每次是O(sqrt(n))的. ...
- 6.3(java学习笔记)缓冲流
一.缓冲流 使用缓冲流后的输入输出流会先存储到缓冲区,等缓冲区满后一次性将缓冲区中的数据写入或取出. 避免程序频繁的和文件直接操作,这样操作有利于提高读写效率. 缓冲流是构建在输入输出流之上的,可以理 ...
- Problem N: 猴子吃桃
#include<stdio.h> int main() { int n,s,i; while(scanf("%d",&n)!=EOF){ s=; ;i> ...
- 在PC机上,如何用Chrome浏览器模拟查看和调试手机的HTML5页面?
如题,如何用PC机上的Chrome浏览器模拟查看和调试手机HTML5页面? 参考操作步骤如下: 第一步.用Chrome打开要调试的页面: 第二步.按F12,打开“开发者工具”,点击其右上角的“Dock ...