iOS:Gif动画功能(显示gif动画、获取gif动画时长、获取gif动画执行次数)
一、简单介绍
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动画执行次数)的更多相关文章
- 【Android端 APP 启动时长获取】启动时长获取方案及具体实施
一.什么是启动时长? 1.启动时长一般包括三种场景,分别是:新装包的首次启动时长,冷启动时长.热启动时长 冷启动 和 热启动 : (1)冷启动:当启动应用时,后台没有该程序的进程,此时启动的话系统会分 ...
- windows server 2008 R2服务器无法通过ShellClass获取mp3音乐时长
我们先看一段代码,获取mp3播放时长: #region GetMediaDetailInfo 获取媒体文件属性信息 /// <summary> /// 获取媒体文件属性信息 /// < ...
- java获取Mp3播放时长
最近有一个用java获取mp3播放时长的需求,有两种,一种本地文件,一种网络文件,其中获取网络mp3播放时间的方法找了挺久终于找到个能用的了. 第一种很简单,下载个jar包 jaudiotagger ...
- Android获取视频音频的时长的方法
android当中获取视频音频的时长,我列举了三种. 1:获取视频URI后获取cursor cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore ...
- Shell32.ShellClass服务器操作系统无法获取 音频文件时长问题
前言: 上传音频文件,自动写入此音频文件的时长,这里用 COM组件Microsoft Shell Controls And Automation来实现. 首先 1.引用:Microsoft Shell ...
- asp.net 获取音视频时长 的方法
http://www.evernote.com/l/AHPMEDnEd65A7ot_DbEP4C47QsPDYLhYdYg/ 日志: 1.第一种方法: 调用:shell32.dll ,win7 ...
- asp.net 获取mp3 播放时长
1 Shell32 //添加引用:COM组件的Microsoft Shell Controls And Automation //然后引用 using Shell32; //如果出现“无法嵌入互操作类 ...
- java 获取音频文件时长
需要导入jar包:jave 1.0.2 jar 如果是maven项目,在pom.xml文件中添加: <dependency> <groupId>it.sauronsoftwar ...
- IOS URL无法对加号进行编码导致http请求时服务器端获取的内容中加号变成空格问题
一.背景. 将以下地址请求服务器时,如果postUrl中某个参数值包含符号+ 那么在服务器获取到这个参数值时,其加号变成了一个空格. NSString *postUrl = "http地址 ...
- C#获取mp3文件时长、解决发布到服务器无法使用问题
首先引用COM组件:Microsoft Shell Controls And Automation,需要引用1.2版本的,1.0的会出问题. 这里需要注意DLL的属性Embed Interop Typ ...
随机推荐
- POJ3076 Sudoku 舞蹈链 DLX
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目(传送门) 题意概括 给出一个残缺的16*16数独,求解. 题解 DLX + 矩阵构建 (两个传送门) 学完这个之后,再 ...
- IDEA学习——模板及其常用模板
模板及其常用模板 (1)psvm (2)sout sout / soutp / soutv / 变量.sout (3)fori iter增强for循环 itar普通for循环 (4)list.for ...
- day19 正则,re模块
http://www.cnblogs.com/Eva-J/articles/7228075.html 所有常用模块的用法 正则的规则: 在一个字符组里面枚举合法的所有字符,字符组里面的任意一个字符和 ...
- Qt错误 —— 无法启动此程序 因为计算机丢失QtCore5.dll 以及 无法定位程序输入点于动态链接库QtCore5.dll
首先,设置计算机的环境变量Path,计算机=>右键属性=>高级设置=>环境变量=>系统变量=>双击Path,在Path后面增加C:\Qt\Qt5.8.0\5.8\ming ...
- 关于cin的缓存区影响效果
1. 当缓冲区中有残留数据时,cin函数会直接去读取这些残留数据而不会请求键盘输入.而且,回车符也会被存入输入缓冲区中. int num{}; while(cin>>num) cout&l ...
- Jmeter实现Basic Auth方式登录
背景 在实际测试日常中,产品有两种登录方式,一种是普通登录方式,另一种则是Basic Auth方式登录.两种登录对应着产品中不同的操作,对权限进行了一个划分. Postman登录 使用Postman登 ...
- Vue项目History模式404问题解决
本文主要解决Vue项目使用History模式发布到服务器Nginx上刷新页面404问题.(由于每个项目的情况都不尽相同,本方案已经完美解决本在所使用项目,具体情况可能还需要修改.) 1.项目背景分析 ...
- UVA 1592 DataBase
思路: 知识补充: ①make_pair和pair: /*pair是将2个数据组合成一个数据,当需要这样的需求时就可以使用pair,如stl中的map就是将key和value放在一起来保存.另一个应用 ...
- BZOJ.3351.[IOI2009]Regions(根号分治 差分)
BZOJ 表示非常爽2333 \(Description\) 给定一棵\(n\)个点的树,每个点有一个属性\(1\leq r_i\leq R\). \(Q\)次询问,每次询问给定\(r1,r2\),求 ...
- Java -- 内部类(一)
什么是内部类 将一个类的定义放在另一个类的定义内部,这就是内部类.在Java中内部类主要分为成员内部类.局部内部类.匿名内部类.静态内部类.举个栗子: public class A { public ...