一、简单介绍

gif动画是iOS开发中很常用的一个功能,有的是为了显示加载视频的过程,更多的是为了显示一个结果状态(动画更直观)。

那么如何执行gif动画,方法有很多。(这里只写一下方法三,前两种之前都用过)

方法一使用UIWebView来显示;

方法二使用UIImageView的帧动画显示;

方法三使用SDWebImage这个三方框架来显示。

二、简单使用

1、显示gif动画(三方框架SDWebImage的UIImage+GIF分类)

#import "UIImage+GIF.h"
#import "SDWebImageGIFCoder.h"
#import "NSImage+WebCache.h" @implementation UIImage (GIF) + (UIImage *)sd_animatedGIFWithData:(NSData *)data {
if (!data) {
return nil;
}
return [[SDWebImageGIFCoder sharedCoder] decodedImageWithData:data];
} - (BOOL)isGIF {
return (self.images != nil);
} @end
//使用
NSData *gifData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"live_sign" ofType:@"gif"]];
UIImage *signSuccessGifImg = [UIImage sd_animatedGIFWithData:gifData];
self.imageView.image = signSuccessGifImg;

2、获取gif动画时长

//获取gif图片的总时长
- (NSTimeInterval)durationForGifData:(NSData *)data{ //将GIF图片转换成对应的图片源
CGImageSourceRef gifSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); //获取其中图片源个数,即由多少帧图片组成
size_t frameCout = CGImageSourceGetCount(gifSource); //定义数组存储拆分出来的图片
NSMutableArray* frames = [[NSMutableArray alloc] init];
NSTimeInterval totalDuration = ;
for (size_t i=; i<frameCout; i++) { //从GIF图片中取出源图片
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); //将图片源转换成UIimageView能使用的图片源
UIImage* imageName = [UIImage imageWithCGImage:imageRef]; //将图片加入数组中
[frames addObject:imageName];
NSTimeInterval duration = [self gifImageDeleyTime:gifSource index:i];
totalDuration += duration;
CGImageRelease(imageRef);
} //获取循环次数
NSInteger loopCount;//循环次数
CFDictionaryRef properties = CGImageSourceCopyProperties(gifSource, NULL);
if (properties) {
CFDictionaryRef gif = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
if (gif) {
CFTypeRef loop = CFDictionaryGetValue(gif, kCGImagePropertyGIFLoopCount);
if (loop) {
//如果loop == NULL,表示不循环播放,当loopCount == 0时,表示无限循环;
CFNumberGetValue(loop, kCFNumberNSIntegerType, &loopCount);
};
}
}
CFRelease(gifSource);
return totalDuration;
}
//获取GIF图片每帧的时长
- (NSTimeInterval)gifImageDeleyTime:(CGImageSourceRef)imageSource index:(NSInteger)index {
NSTimeInterval duration = ;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, NULL);
if (imageProperties) {
CFDictionaryRef gifProperties;
BOOL result = CFDictionaryGetValueIfPresent(imageProperties, kCGImagePropertyGIFDictionary, (const void **)&gifProperties);
if (result) {
const void *durationValue;
if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFUnclampedDelayTime, &durationValue)) {
duration = [(__bridge NSNumber *)durationValue doubleValue];
if (duration < ) {
if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFDelayTime, &durationValue)) {
duration = [(__bridge NSNumber *)durationValue doubleValue];
}
}
}
}
} return duration;
}

3、获取gif动画执行次数

//获取gif图片的循环次数
-(NSInteger)repeatCountForGifData:(NSData *)data{ //将GIF图片转换成对应的图片源
CGImageSourceRef gifSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); //获取循环次数
NSInteger loopCount = ;//循环次数
CFDictionaryRef properties = CGImageSourceCopyProperties(gifSource, NULL);
if (properties) {
CFDictionaryRef gif = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
if (gif) {
CFTypeRef loop = CFDictionaryGetValue(gif, kCGImagePropertyGIFLoopCount);
if (loop) {
//如果loop == NULL,表示不循环播放,当loopCount == 0时,表示无限循环;
CFNumberGetValue(loop, kCFNumberNSIntegerType, &loopCount);
};
}
}
CFRelease(gifSource); return loopCount;
}

三、创建分类

#import "UIImageView+GIFProperty.h"

//
// UIImageView+GIFProperty.h
// YuwenParent
//
// Created by 夏远全 on 2019/4/11.
// Copyright © 2019年 xiaoshuang. All rights reserved.
// #import <UIKit/UIKit.h> NS_ASSUME_NONNULL_BEGIN @interface UIImageView (GIFProperty)
-(NSTimeInterval)durationForGifData:(NSData *)data;//获取gif动画总时长
-(NSInteger)repeatCountForGifData:(NSData *)data; //获取gif动画循环总次数 0:表示无限循环
@end NS_ASSUME_NONNULL_END
//
// UIImageView+GIFProperty.m
// YuwenParent
//
// Created by 夏远全 on 2019/4/11.
// Copyright © 2019年 xiaoshuang. All rights reserved.
// #import "UIImageView+GIFProperty.h" @implementation UIImageView (GIFProperty) //获取gif图片的总时长
- (NSTimeInterval)durationForGifData:(NSData *)data{ //将GIF图片转换成对应的图片源
CGImageSourceRef gifSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); //获取其中图片源个数,即由多少帧图片组成
size_t frameCout = CGImageSourceGetCount(gifSource); //定义数组存储拆分出来的图片
NSMutableArray* frames = [[NSMutableArray alloc] init];
NSTimeInterval totalDuration = ;
for (size_t i=; i<frameCout; i++) { //从GIF图片中取出源图片
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL); //将图片源转换成UIimageView能使用的图片源
UIImage* imageName = [UIImage imageWithCGImage:imageRef]; //将图片加入数组中
[frames addObject:imageName];
NSTimeInterval duration = [self gifImageDeleyTime:gifSource index:i];
totalDuration += duration;
CGImageRelease(imageRef);
} //获取循环次数
NSInteger loopCount;//循环次数
CFDictionaryRef properties = CGImageSourceCopyProperties(gifSource, NULL);
if (properties) {
CFDictionaryRef gif = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
if (gif) {
CFTypeRef loop = CFDictionaryGetValue(gif, kCGImagePropertyGIFLoopCount);
if (loop) {
//如果loop == NULL,表示不循环播放,当loopCount == 0时,表示无限循环;
CFNumberGetValue(loop, kCFNumberNSIntegerType, &loopCount);
};
}
}
CFRelease(gifSource);
return totalDuration;
} //获取gif图片的循环次数
-(NSInteger)repeatCountForGifData:(NSData *)data{ //将GIF图片转换成对应的图片源
CGImageSourceRef gifSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); //获取循环次数
NSInteger loopCount = ;//循环次数
CFDictionaryRef properties = CGImageSourceCopyProperties(gifSource, NULL);
if (properties) {
CFDictionaryRef gif = CFDictionaryGetValue(properties, kCGImagePropertyGIFDictionary);
if (gif) {
CFTypeRef loop = CFDictionaryGetValue(gif, kCGImagePropertyGIFLoopCount);
if (loop) {
//如果loop == NULL,表示不循环播放,当loopCount == 0时,表示无限循环;
CFNumberGetValue(loop, kCFNumberNSIntegerType, &loopCount);
};
}
}
CFRelease(gifSource); return loopCount;
} //获取GIF图片每帧的时长
- (NSTimeInterval)gifImageDeleyTime:(CGImageSourceRef)imageSource index:(NSInteger)index {
NSTimeInterval duration = ;
CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, index, NULL);
if (imageProperties) {
CFDictionaryRef gifProperties;
BOOL result = CFDictionaryGetValueIfPresent(imageProperties, kCGImagePropertyGIFDictionary, (const void **)&gifProperties);
if (result) {
const void *durationValue;
if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFUnclampedDelayTime, &durationValue)) {
duration = [(__bridge NSNumber *)durationValue doubleValue];
if (duration < ) {
if (CFDictionaryGetValueIfPresent(gifProperties, kCGImagePropertyGIFDelayTime, &durationValue)) {
duration = [(__bridge NSNumber *)durationValue doubleValue];
}
}
}
}
} return duration;
} @end

iOS:Gif动画功能(显示gif动画、获取gif动画时长、获取gif动画执行次数)的更多相关文章

  1. 【Android端 APP 启动时长获取】启动时长获取方案及具体实施

    一.什么是启动时长? 1.启动时长一般包括三种场景,分别是:新装包的首次启动时长,冷启动时长.热启动时长 冷启动 和 热启动 : (1)冷启动:当启动应用时,后台没有该程序的进程,此时启动的话系统会分 ...

  2. windows server 2008 R2服务器无法通过ShellClass获取mp3音乐时长

    我们先看一段代码,获取mp3播放时长: #region GetMediaDetailInfo 获取媒体文件属性信息 /// <summary> /// 获取媒体文件属性信息 /// < ...

  3. java获取Mp3播放时长

    最近有一个用java获取mp3播放时长的需求,有两种,一种本地文件,一种网络文件,其中获取网络mp3播放时间的方法找了挺久终于找到个能用的了. 第一种很简单,下载个jar包  jaudiotagger ...

  4. Android获取视频音频的时长的方法

    android当中获取视频音频的时长,我列举了三种. 1:获取视频URI后获取cursor cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore ...

  5. Shell32.ShellClass服务器操作系统无法获取 音频文件时长问题

    前言: 上传音频文件,自动写入此音频文件的时长,这里用 COM组件Microsoft Shell Controls And Automation来实现. 首先 1.引用:Microsoft Shell ...

  6. asp.net 获取音视频时长 的方法

    http://www.evernote.com/l/AHPMEDnEd65A7ot_DbEP4C47QsPDYLhYdYg/ 日志:   1.第一种方法:   调用:shell32.dll ,win7 ...

  7. asp.net 获取mp3 播放时长

    1 Shell32 //添加引用:COM组件的Microsoft Shell Controls And Automation //然后引用 using Shell32; //如果出现“无法嵌入互操作类 ...

  8. java 获取音频文件时长

    需要导入jar包:jave 1.0.2 jar 如果是maven项目,在pom.xml文件中添加: <dependency> <groupId>it.sauronsoftwar ...

  9. IOS URL无法对加号进行编码导致http请求时服务器端获取的内容中加号变成空格问题

    一.背景. 将以下地址请求服务器时,如果postUrl中某个参数值包含符号+  那么在服务器获取到这个参数值时,其加号变成了一个空格. NSString *postUrl = "http地址 ...

  10. C#获取mp3文件时长、解决发布到服务器无法使用问题

    首先引用COM组件:Microsoft Shell Controls And Automation,需要引用1.2版本的,1.0的会出问题. 这里需要注意DLL的属性Embed Interop Typ ...

随机推荐

  1. Linux centos7安装python3并且不影响python2

    一.安装依赖 yum -y groupinstall "Development tools" yum -y install zlib-devel bzip2-devel opens ...

  2. JS uint8Array转String

    Uint8Array转字符串 function Uint8ArrayToString(fileData){ var dataString = ""; ; i < fileDa ...

  3. Oozie

    Oozie的功能模块 workflow 由多个工作单元组成 工作单元之间有依赖关系 MR1->MR2->MR3->result hadoop jar:提交1个MR oozie:监控当 ...

  4. VS2013 FFmpeg开发环境配置

    1.下载ffmpeg包(dll.include.lib)   https://ffmpeg.zeranoe.com/builds/         有3个版本:Static.Shared和Dev St ...

  5. datatables出现横向滚动条

    datatables会扩大表单的宽度,设置为table-responsive 自适应的时候则会出现滚动条.

  6. 初心不负 笔记-JS高级程序设计-引用类型篇-Array

    ES3方法集合: 1join()方法,将一个数组里面的所有元素转换成字符串,然后再将他们连接起来返回一个字符串,通过制定的符号,默认值为逗号.不会改变原数组 ,,,,]; a.join(); &quo ...

  7. linux学习笔记 其他常用命令

    cd + 回车  = cd ~ 进入当前用户主目录 查看指定进程信息 *ps -ef |grep 进程名 *ps -查看属于自己的进程 *ps -aux 查看所有的用户的执行进程 换成    ps - ...

  8. SQL varbinary varchar 互转

    --============================================== -- FUNCTION varbin2hexstr -- 将 varbinary 类型的数据转换为 v ...

  9. [Python]Python入坑小项目推荐- Flask example minitwit

    知乎上看到的Python练手项目推荐,链接见:https://www.zhihu.com/question/29372574,不知道是我自己懒得看还是理解力不行,这些项目真的是...太大了呀~~~~ ...

  10. OI考试需注意的

    能用结构体就用结构体,特别是队列之类的数据结构:类别相同的变量或数组名字不要太相近,最好在名字后面加上标识符(e.g:hash[]&HASH[]就不好,hash1[]&hash2[]正 ...