问题

有时候需要显示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. 拿了十几个offer,怎样做选择?

    本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 最近收到好几个读者的咨询,关于如何选offer的 ...

  2. APP后台架构20191205

    1.架构,架构与业务紧密相关,是有业务驱动的. 2.APP后台演进原则. App后台的架构是由业务规模驱动而演进的,App后台是为业务服务的,App后台的价值在于能为业务提供其所需要的功能,不应过度设 ...

  3. Java数据结构-02单链表(一)

    一.链式存储: ①简述:线性表的链式存储结构的特点是用一组任意的存储单元存储线性表的数据元素,这组存储单元可以是连续的,也可以是不连续的.存储单元由两部分组成,数据源和指针,数据源放数据,指针指向下个 ...

  4. viewpage轮播图

    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com ...

  5. Zabbix批量添加Hosts

    添加脚本 addhost.py #coding:utf-8 import json import urllib2 from urllib2 import URLError import sys imp ...

  6. [Luogu P3157][CQOI2011]动态逆序对 (树套树)

    题面 传送门:[CQOI2011]动态逆序对 Solution 一开始我看到pty巨神写这套题的时候,第一眼还以为是个SB题:这不直接开倒车线段树统计就完成了吗? 然后冷静思考了一分钟,猛然发现单纯的 ...

  7. numpy数组

    一.数组创建 基础数组 1.array() array函数可以创建一维或多维数 一维数组 1.arange(起始值,终值,步长) 2.linspace(起始值,终值,元素个数) --创建等步长的数组 ...

  8. python开发基础(二)运算符以及数据类型之tuple(元组)

    # encoding: utf-8 # module builtins # from (built-in) # by generator 1.147 """ Built- ...

  9. learning to Estimate 3D Hand Pose from Single RGB Images论文理解

    持续更新...... 概括:以往很多论文借助深度信息将2D上升到3D,这篇论文则是想要用网络训练代替深度数据(设备成本比较高),提高他的泛性,诠释了只要合成数据集足够大和网络足够强,我就可以不用深度信 ...

  10. Azure Data Factory(四)集成 Logic App 的邮件通知提醒

    一,引言 上一篇有介绍到使用Azure Data Factory 复制数据,然后有集成 Azure DevOps 实现CI/CD,但是对于真正的项目来说,这些肯定是不够的,比如说在执行 Azure P ...