iOS Gif图片展示N种方式(原生+第三方)_IOS

本文分享了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图片
第三方展示监控链接、第三方展示监测链接、第三方产品展示网站、ios gif第三方、ios 展示gif,以便于您获取更多的相关知识。

时间: 2024-09-20 09:12:42

iOS Gif图片展示N种方式(原生+第三方)_IOS的相关文章

详解iOS获取通讯录的4种方式_IOS

本文实例为大家分享了iOS获取通讯录的4种方式,供大家参考,具体内容如下 使用场景 一些App通过手机号码来推荐好友,如 微博.支付宝 首先客户端会获取通讯录中的所有手机号然后将这些手机号提交到App服务器中,服务器会查找每个手机号对应的App账号如QQ号码返回到客户端,然后客户端根据服务器返回的账号列表来推荐好友. 获取联系人方式 方案一:AddressBookUI.framework框架 提供了联系人列表界面.联系人详情界面.添加联系人界面等 一般用于选择联系人 方案二:AddressBoo

Android图片压缩几种方式总结

Android图片压缩几种方式总结 图片压缩在Android开发中很常见也很重要,防止图片的OOM也是压缩的重要原因. 首先看下Bitmap图片文件的大小的决定因素: Bitmap所占用的内存 = 图片长度 x 图片宽度 x 一个像素点占用的字节数.3个参数,任意减少一个的值,就达到了压缩的效果. 接下来看下Bitmap图片的几种格式的特点: ALPHA_8  表示8位Alpha位图,即A=8,一个像素点占用1个字节,它没有颜色,只有透明度  ARGB_4444 表示16位ARGB位图,即A=4

android绘制圆形图片的两种方式示例

android绘制圆形图片的两种方式 看下效果先 下面有完整的示例代码 使用BitmapShader(着色器) 我们在绘制view 的时候 就是小学上美术课 用水彩笔在本子上画画 使用着色器绘制圆形图片最简单的理解方式 就是把bitmap当做一种颜色 设置给paint ,paint都已经有颜色了 你想让它方了,圆了,扁了 还不是看你心情 canvas调用那个方法咯 实现的大致思路如下: 1. 创建一个类 继承imageView 重写onDraw() 2. 获取到bitmap图片 3. 计算图片的

Android实现圆形图片的两种方式_Android

在项目中,我们经常会用到圆形图片,但是android本身又没有提供,那我只能我们自己来完成. 第一种方式,自定义CircleImageView: public class CircleImageView extends ImageView { private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Con

Android实现圆形图片的两种方式

在项目中,我们经常会用到圆形图片,但是android本身又没有提供,那我只能我们自己来完成. 第一种方式,自定义CircleImageView: public class CircleImageView extends ImageView { private static final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP; private static final Bitmap.Config BITMAP_CONFIG = Bitmap.Con

IOS开发:Unity3D 两种方式播放游戏视频

  Unity支持的播放视频格式有.mov..mpg..mpeg..mp4..avi和.asf.只需将对应的视频文件拖拽入Project视图即可,它会自动生成对应的MovieTexture对象.如下图所示,MOMO将default_video.mp4拖拽入Project视图中,如果视频中含有音频的话会对应生成audio文件,因为我的视频没有音频所以没有生成 audio文件.接着在Hierarchy视图中创建一个Plane对象视频将在它之上播放,Directional light世界定向光用于照亮

iOS利用Block逆向传值的方式详解_IOS

前言 在iOS通过代理逆向传值的方式详解一文中,分析了如何利用代理模式来逆向传值,其实还有一些其他的方式,如通知.Block等,相比较代理,我个人认为反而要简单些,但是需要处理好细节问题,如Block循环引用.还是用前文的案例,本次使用Block来实现,Block的基本知识本文不再赘述. 一.书写规范 Block传值,需要注意的是,谁传值就需要定义Block,捕获方仅仅需要传递Block给传值方,并处理捕获的值. 传值方      1.定义Block用于传值      2.声明一个上述Block

对比分析iOS延迟执行的4种方式_IOS

最近学习了延迟执行的几种方法,分享一下: 1.performSelector(NSObject)方法  2.NSTimer方法  3.GCD方法  4.sleep(NSThread)方法 一.performSelector方法: 复制代码 代码如下: [self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.0f]; 1.特点: 此方式要求必须在主线程中执行,否则无效. 是一种非阻塞的执行方式, 暂时未找到取

iOS通过代理逆向传值的方式详解_IOS

前言 在iOS开发中,常见的几种逆向传值方式,有代理(delegate).通知(NSNotification),block等等,本文就给大家分析下,如何理解和快速上手代理模式,并以一个简单的逆向传值为案例,看看代理模式是不是很难. 一.代理模式中的几个概念 讲代理模式之前,首先需要弄清楚两个概念:被代理对象和代理对象.并且需要知道它们之间是靠协议关连起来的. 1.被代理对象 被代理对象往往就是真正有做事意图的那个对象,比如卖房子案例中的想卖房子的房主,保姆婴儿案例中想喝奶的婴儿.但是它们自己做不