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

 更新时间:2016年03月03日 11:59:25   投稿:lijiao  
这篇文章主要介绍了iOS Gif图片展示N种方式,包括原生、第三方方式展示,感兴趣的小伙伴们可以参考一下

本文分享了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开发自定义页脚和页眉技巧详解

    iOS开发自定义页脚和页眉技巧详解

    这篇文章主要为大家介绍了iOS开发自定义页脚和页眉的技巧示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-07-07
  • IOS中手风琴表不能移动的解决方法

    IOS中手风琴表不能移动的解决方法

    本文给大家分享的是IOS开发中遇到的一个手风琴特效无法正常工作的问题的解决方法,经过度娘了很久才找到解决方法,这里推荐给大家,有需要的小伙伴可以参考下。
    2015-05-05
  • Objective-C与Swift之间的互相调用和跳转

    Objective-C与Swift之间的互相调用和跳转

    这篇文章主要给大家介绍了关于Objective-C与Swift之间的互相调用和跳转的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-05-05
  • iOS WKWebView中MessageHandler内存泄漏问题的完美解决过程

    iOS WKWebView中MessageHandler内存泄漏问题的完美解决过程

    这篇文章主要给大家介绍了关于iOS WKWebView中MessageHandler内存泄漏问题的完美解决过程,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • iOS实现转盘效果

    iOS实现转盘效果

    这篇文章主要为大家详细介绍了iOS实现转盘效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-04-04
  • iOS使用自带的UIViewController实现qq加号下拉菜单的功能(实例代码)

    iOS使用自带的UIViewController实现qq加号下拉菜单的功能(实例代码)

    这篇文章主要介绍了iOS使用自带的UIViewController实现qq加号下拉菜单的功能(实例代码),需要的朋友可以参考下
    2017-05-05
  • Swift中的HTTP请求体Request Bodies使用示例详解

    Swift中的HTTP请求体Request Bodies使用示例详解

    这篇文章主要为大家介绍了Swift中的HTTP请求体Request Bodies使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-02-02
  • iOS自定义选择框代码分享

    iOS自定义选择框代码分享

    这篇文章主要为大家分享了iOS自定义选择框的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-03-03
  • iOS的UI开发中Modal的使用与主流应用UI结构介绍

    iOS的UI开发中Modal的使用与主流应用UI结构介绍

    这篇文章主要介绍了iOS的UI开发中Modal的使用与主流应用UI结构,代码基于传统的Objective-C,需要的朋友可以参考下
    2015-12-12
  • Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件

    Flutter Widgets粘合剂CustomScrollView NestedScrollVie

    这篇文章主要为大家介绍了Flutter Widgets粘合剂CustomScrollView NestedScrollView滚动控件示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-11-11

最新评论