一、简单介绍

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. xml方式将dataset导出excel

    using System;using System.Collections;using System.Collections.Generic;using System.Data;using Syste ...

  2. kmp基础 ekmp

    +]; int lenp,lens; +];//可以是char 也可以是string +]; void getnext() { nex[]=-; ,j=; ) { ||p[j]==p[k]) nex[ ...

  3. 深入理解Python中赋值、深拷贝(deepcopy)、浅拷贝(copy)

    赋值 python跟java中的变量本质是不一样的,Python的变量实质上是一个指针(int型或str型),而java的变量是一个可操作的存储空间. a = 123b = a print(id(a) ...

  4. day56 文件 文档处理,事件

    前情回顾: 1. 前情回顾 0. 选择器补充 - 属性选择器 - $("[egon]") - $("[type='text']") - $("inpu ...

  5. --mysql 导出数据时, 数字类型的列如果位数过长,变为科学计数法问题

    --mysql 导出数据时, 数字类型的列如果位数过长,变为科学计数法问题在字段前加上\t即可select concat('\t',a.IDCARD_NO) from xxx a

  6. 比特币源码分析--C++11和boost库的应用

    比特币源码分析--C++11和boost库的应用     我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...

  7. 使用Chrome浏览器设置XX-net的方法

        以下介绍使用Chrome浏览器设置XX-net的方法 1.下载并安装谷歌浏览器. 2.打开https://github.com/XX-net/XX-Net/blob/master/code/d ...

  8. linux 学习笔记 mysql安装总结

    1 安装方式 下载2禁制源码安装包 mysql-5.5.27-linux2.6-i686.tar.gz 备注:2禁制额包解压缩后直接就可以使用 不用Make 2 步骤 shell>groupad ...

  9. maven 构建spring boot + mysql 的基础项目

    一.maven 依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...

  10. 彻底理解this 的值到底是什么?

    作者:方应杭 来源:知乎 你可能遇到过这样的 JS 面试题: var obj = { foo: function(){ console.log(this) } } var bar = obj.foo ...