问题

有时候需要显示gif动态图,让界面更加的绚丽,但是iOS默认只支持png,gpg图片。那么如何才能显示gif图呢?

解决方式

  1. 添加框架

    CoreGraphics.framework

    ImageIO.framework

  2. 引入SCGIFImageView类

SCGIFImageView.h

    #import <UIKit/UIKit.h>

    @interface SCGIFImageFrame : NSObject {

    }
@property (nonatomic) double duration;
@property (nonatomic, retain) UIImage* image; @end @interface SCGIFImageView : UIImageView {
NSInteger _currentImageIndex;
}
@property (nonatomic, retain) NSArray* imageFrameArray;
@property (nonatomic, retain) NSTimer* timer;
@property (nonatomic, assign) NSInteger sumCount; //Setting this value to pause or continue animation;
@property (nonatomic) BOOL animating; - (void)setData:(NSData*)imageData;
@end

SCGIFImageView.m

    #import "SCGIFImageView.h"
#import <ImageIO/ImageIO.h> @implementation SCGIFImageFrame
@synthesize image = _image;
@synthesize duration = _duration; @end @interface SCGIFImageView () - (void)resetTimer; - (void)showNextImage; @end @implementation SCGIFImageView
@synthesize imageFrameArray = _imageFrameArray;
@synthesize timer = _timer;
@synthesize animating = _animating; - (void)resetTimer {
if (_timer && _timer.isValid) {
[_timer invalidate];
} self.timer = nil;
} - (void)setData:(NSData *)imageData {
if (!imageData) {
return;
}
[self resetTimer];
CGImageSourceRef source = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
size_t count = CGImageSourceGetCount(source); NSMutableArray* tmpArray = [NSMutableArray array]; for (size_t i = 0; i < count; i++) {
SCGIFImageFrame* gifImage = [[SCGIFImageFrame alloc] init]; CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
gifImage.image = [UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]; NSDictionary* frameProperties = CFBridgingRelease(CGImageSourceCopyPropertiesAtIndex(source, i, NULL));
gifImage.duration = [[[frameProperties objectForKey:(NSString*)kCGImagePropertyGIFDictionary] objectForKey:(NSString*)kCGImagePropertyGIFDelayTime] doubleValue];
gifImage.duration = MAX(gifImage.duration, 0.01); [tmpArray addObject:gifImage]; CGImageRelease(image);
}
CFRelease(source); self.imageFrameArray = nil;
if (tmpArray.count > 1) {
self.imageFrameArray = tmpArray;
_currentImageIndex = -1;
_animating = YES;
[self showNextImage];
} else {
self.image = [UIImage imageWithData:imageData];
}
} - (void)setImage:(UIImage *)image {
[super setImage:image];
[self resetTimer];
self.imageFrameArray = nil;
_animating = NO;
} - (void)showNextImage {
if (_sumCount > 0) {
_animating = _sumCount - 1 == _currentImageIndex ? NO : YES;
}
if (!_animating) {
return;
}
_currentImageIndex = (++_currentImageIndex) % _imageFrameArray.count;
SCGIFImageFrame* gifImage = [_imageFrameArray objectAtIndex:_currentImageIndex];
[super setImage:[gifImage image]];
self.timer = [NSTimer scheduledTimerWithTimeInterval:gifImage.duration target:self selector:@selector(showNextImage) userInfo:nil repeats:NO];
} - (void)setAnimating:(BOOL)animating {
if (_imageFrameArray.count < 2) {
_animating = animating;
return;
} if (!_animating && animating) {
//continue
_animating = animating;
if (!_timer) {
[self showNextImage];
}
} else if (_animating && !animating) {
//stop
_animating = animating;
[self resetTimer];
}
} @end
  1. 添加gif图片

    在Images.xcassets中添加需要的gif动态图。

  2. 使用方式

    • 引入头文件SCGIFImageView.h
    • 在需要的添加的地方添加以下代码

         NSString* filePath = [[NSBundle mainBundle] pathForResource:@"6plus_hover.gif" ofType:nil];
      NSData* imageData = [NSData dataWithContentsOfFile:filePath]; SCGIFImageView* gifImageView = [[SCGIFImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 320)];
      [gifImageView setData:imageData];
      [gifImageView setSumCount:gifImageView.imageFrameArray.count];//设置只循环一次,不设置无限循环
      [self.view addSubview:gifImageView];

h1, h2, h3, h4, h5, h6, p, blockquote { margin: 0; padding: 0 }
body { font-family: "Helvetica Neue", Helvetica, "Hiragino Sans GB", Arial, sans-serif; font-size: 13px; line-height: 18px; color: rgba(115, 115, 115, 1); background-color: rgba(255, 255, 255, 1); margin: 10px 13px }
table { margin: 10px 0 15px; border-collapse: collapse }
td, th { border: 1px solid rgba(221, 221, 221, 1); padding: 3px 10px }
th { padding: 5px 10px }
a { color: rgba(0, 105, 214, 1) }
a:hover { color: rgba(0, 80, 163, 1); text-decoration: none }
a img { border: none }
p { margin-bottom: 9px }
h1, h2, h3, h4, h5, h6 { color: rgba(64, 64, 64, 1); line-height: 36px }
h1 { margin-bottom: 18px; font-size: 30px }
h2 { font-size: 24px }
h3 { font-size: 18px }
h4 { font-size: 16px }
h5 { font-size: 14px }
h6 { font-size: 13px }
hr { margin: 0 0 19px; border-top: 0; border-right: 0; border-bottom: 1px solid rgba(204, 204, 204, 1); border-left: 0 }
blockquote { padding: 13px 13px 21px 15px; margin-bottom: 18px; font-family: georgia, serif; font-style: italic }
blockquote:before { content: "“"; font-size: 40px; margin-left: -10px; font-family: georgia, serif; color: rgba(238, 238, 238, 1) }
blockquote p { font-size: 14px; font-weight: 300; line-height: 18px; margin-bottom: 0; font-style: italic }
code, pre { font-family: Monaco, Andale Mono, Courier New, monospace }
code { background-color: rgba(254, 233, 204, 1); color: rgba(0, 0, 0, 0.75); padding: 1px 3px; font-size: 12px; -webkit-border-radius: 3px; -moz-border-radius: 3px; border-radius: 3px }
pre { display: block; padding: 14px; margin: 0 0 18px; line-height: 16px; font-size: 11px; border: 1px solid rgba(217, 217, 217, 1); white-space: pre-wrap; word-wrap: break-word }
pre code { background-color: rgba(255, 255, 255, 1); color: rgba(115, 115, 115, 1); font-size: 11px; padding: 0 }
sup { font-size: 0.83em; vertical-align: super; line-height: 0 }
* { -webkit-print-color-adjust: exact }
@media screen and (min-width: 914px) { body { width: 854px; margin: 10px auto } }
@media print { body, code, pre code, h1, h2, h3, h4, h5, h6 { color: rgba(0, 0, 0, 1) } table, pre { page-break-inside: avoid } }

iOS gif图显示问题的更多相关文章

  1. Ios 该图显示其出现的相关问题定义UITableView dataSource must return a cell from tableView:cellForRowAtIndexPath:&#39;

    解决这个问题 在 加上个 标示符 Cell 自己定义 customCell .h 代码例如以下 ViewController.m 文件里 代码例如以下 执行结果 吕 图坚持直接在这里 不行

  2. iOS启动图和开屏广告图,类似网易

    iOS启动图和开屏广告图,类似网易 启动图是在iOS开发过程中必不可少的一个部分,很多app在启动图之后会有一张自定义的开屏广告图,点击该广告图可以跳转到广告图对应的页面.今天呢,和大家分享一下如何添 ...

  3. Xamarin iOS教程之显示和编辑文本

    Xamarin iOS教程之显示和编辑文本 Xamarin iOS显示和编辑文本 在一个应用程序中,文字是非常重要的.它就是这些不会说话的设备的嘴巴.通过这些文字,可以很清楚的指定这些应用程序要表达的 ...

  4. storyboard在ios模拟器无法显示的问题

    一.问题描述 1.在原有项目新建一个名称为test的storyboard类型的文件. 2.test.storyboard添加View Controller,并设置View Controller下Vie ...

  5. 利用matlab编写实现显示fmri切片slice图像 混合显示 不同侧面显示 可叠加t检验图显示 by DR. Rajeev Raizada

    1.参考 reference 1. tutorial主页:http://www.bcs.rochester.edu/people/raizada/fmri-matlab.htm. 2.speech_b ...

  6. iOS多图上传

    iOS多图上传涉及到多线程问题,个人比较喜欢使用GCD操作,下边是最近写的一个多图上传代码,附带相关注释 __block BOOL allSucc = YES; __block int m = 0; ...

  7. 微信小程序开发--背景图显示

    这两天开发微信小程序,在设置背景图片时,发现在wxss里面设置 background-image:(url) 属性,不管是开发工具还是线上都无法显示.经过查资料发现,background-image ...

  8. Linux命令之pstree - 以树状图显示进程间的关系

    pstree命令以树状图显示进程间的关系(display a tree of processes).ps命令可以显示当前正在运行的那些进程的信息,但是对于它们之间的关系却显示得不够清晰.在Linux系 ...

  9. [Linux] Linux命令之pstree - 以树状图显示进程间的关系

    转载自: http://codingstandards.iteye.com/blog/842156 pstree命令以树状图显示进程间的关系(display a tree of processes). ...

随机推荐

  1. (Pytorch)涉及的常见操作

    涉及一些pytorch的API内容在此进行整理 损失函数:Binary-Cross-Entropy loss criterion = nn.BCECriterion() 创建一个标准来度量目标和输出之 ...

  2. Java踩坑记系列之Arrays.AsList

    java.util.Arrays的asList方法可以方便的将数组转化为集合,我们平时开发在初始化ArrayList时使用的比较多,可以简化代码,但这个静态方法asList()有几个坑需要注意: 一. ...

  3. Java学习的第四十四天

    1.例5.4将二维数组的行列互换 public class cjava { public static void main(String []args) { int [][]a=new int [][ ...

  4. 03.axios登录前端

    1.创建一个Login.vue页面   1.1 写页面 views/Login.vue   在 views/components 下创建 Login.vue 页面   <template> ...

  5. DM存储过程示例子-表的行数对比

    1.DM存储过程示例子,表的行数对比 1 --1. ================全量 2 --select * from DM_VERI where c2 != c3; 3 --drop tabl ...

  6. 一款基于.NET Core的认证授权解决方案-葫芦藤1.0开源啦

    背景 18年公司准备在技术上进行转型,而公司技术团队是互相独立的,新技术的推动阻力很大.我们需要找到一个切入点.公司的项目很多,而各个系统之间又不互通,导致每套系统都有一套登录体系,给员工和客户都带来 ...

  7. php 之批量生成 mysql 语句 注释

    工作需要 需要更新 所有表的注释 不影响原结构之上进行更新 <?php class Ceshi extends CI_Controller{ function __construct() { p ...

  8. 使用配置文件方式记录Python程序日志

    开发者可以通过三种方式配置日志记录: 调用配置方法的Python代码显式创建记录器.处理程序和格式化程序. 创建日志配置文件并使用fileConfig() 函数读取. 创建配置信息字典并将其传递给di ...

  9. [leetcode/lintcode 题解] 微软 面试题:实现 Trie(前缀树)

    实现一个 Trie,包含 ​insert​, ​search​, 和 ​startsWith​ 这三个方法.   在线评测地址:领扣题库官网     样例 1: 输入:    insert(" ...

  10. UNP——第三章,套接字编程介绍

    1.套接字结构 多数套接字函数都有套接字结构参数,每个协议族都定义了自己的套接字结构,以 sockaddr_ 开始,并对应协议族的唯一后缀. struct sockaddr_in { uint8_t ...