问题

有时候需要显示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. windows上visual studio 2019配置libtorch

    参考链接: https://blog.csdn.net/clx55555/article/details/98172762 https://zhuanlan.zhihu.com/p/68901339 ...

  2. 【总结】nginx基础

    一.nginx简介 1.什么是nginx? Nginx 是高性能的 HTTP 和反向代理的服务器,处理高并发能力是十分强大的,支持高达 50,000 个并发连接数.功能:反向代理,负载均衡,动静分离 ...

  3. 云计算管理平台之OpenStack网络服务neutron

    一.简介 neutron的主要作用是在openstack中为启动虚拟机实例提供网络服务,对于neutron来讲,它可以提供两种类型的网络:第一种是provider network,这种网络就是我们常说 ...

  4. mshadow入门指南

    mshadow是一个基于表达式模板实现的张量库,在MXNet框架中被广泛使用.这篇文章简单介绍了mshadow的基本用法和特性,文章主要翻译自mshadow/guide/README. 张量数据结构 ...

  5. Java学习的第二十三天

    1.今天学习了用log4j记录日志 综合实例 2.不知道日志什么时候用到 3.明天学习12章

  6. Viper 微服务框架 编写一个hello world 插件-02

    1.Viper是什么? Viper 是.NET平台下的Anno微服务框架的一个示例项目.入门简单.安全.稳定.高可用.全平台可监控.底层通讯可以随意切换thrift grpc. 自带服务发现.调用链追 ...

  7. python爬虫使用xpath解析页面和提取数据

    XPath解析页面和提取数据 一.简介 关注公众号"轻松学编程"了解更多. XPath即为XML路径语言,它是一种用来确定XML(标准通用标记语言的子集)文档中某部分位置的语言.X ...

  8. uniapp微信小程序canvas隐藏

    HTML 我是把canvas嵌套在view里并被view设置id <view id="canvas"> <canvas style="width: 35 ...

  9. 正式班D23

    2020.11.05星期四 正式班D23 目录 12.3.3 HUP信号 12.3.3 HUP信号 在关闭终端时,终端会收到Linux HUP信号(hangup信号),关闭其所有子进程. 想让进程一直 ...

  10. Hive Join优化

    在阐述Hive Join具体的优化方法之前,首先看一下Hive Join的几个重要特点,在实际使用时也可以利用下列特点做相应优化: 1. 只支持等值连接 2. 底层会将写的HQL语句转换为MapRed ...