UIImageView可以添加gif动画制作吗

拒绝访问 |
| 百度云加速
请打开cookies.
此网站 () 的管理员禁止了您的访问。原因是您的访问包含了非浏览器特征(3aa77dbc075543cb-ua98).
重新安装浏览器,或使用别的浏览器10:06 提问
iphone-UIButton中的动画gif
我写的代码运行之后,160*148尺寸的图片变的非常大,全屏了……
UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
animatedImageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@"button1.png"],
[UIImage imageNamed:@"button2.png"],
[UIImage imageNamed:@"button3.png"],
[UIImage imageNamed:@"button4.png"], nil];
animatedImageView.animationDuration = 1.0f;
animatedImageView.animationRepeatCount = 0;
[animatedImageView startAnimating];
[yourButton addSubview: animatedImageView];
这个应该怎么解决?
按赞数排序
楼主在初始化UIImageView的尺寸时 是否设置错了frame
imageview 属性设置呢1234567
其他相关推荐iOS开发怎么实现显示gif图片例子方法 - 维维软件园
iOS开发怎么实现显示gif图片例子方法
来源:本站整理作者:佚名时间: 13:32:00(0)
iOS开发怎么实现显示gif图片例子方法详解,Gif是由一阵阵画面组成的,并且每一帧画面播放的时长可能会不一样,于是自己写一个解析Gif的工具类,解决每一帧画面并遵循每一帧所对应的显示时间进行播放。
  程序思路:
  1、首先使用ImageIO库中的CGImageSource家在Gif文件。
  2、通过CGImageSource获取到Gif文件中的总的帧数,以及每一帧的显示时间。
  3、通过CAKeyframeAnimation来完成Gif动画的播放。
  下面直接上我写的解析和播放Gif的工具类的代码:
// SvGifView.h
// SvGifSample
// Created by maple on 3/28/13.
// Copyright (c) 2013 smileEvday. All rights reserved.
#import &UIKit/UIKit.h&
@interface SvGifView : UIView
* @brief desingated initializer
- (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
* @brief start Gif Animation
- (void)startG
* @brief stop Gif Animation
- (void)stopG
* @brief get frames image(CGImageRef) in Gif
+ (NSArray*)framesInGif:(NSURL*)fileURL;
// SvGifView.m
// SvGifSample
// Created by maple on 3/28/13.
// Copyright (c) 2013 smileEvday. All rights reserved.
#import &SvGifView.h&
#import &ImageIO/ImageIO.h&
#import &QuartzCore/CoreAnimation.h&
* @brief resolving gif information
void getFrameInfo(CFURLRef url, NSMutableArray *frames, NSMutableArray *delayTimes, CGFloat *totalTime,CGFloat *gifWidth, CGFloat *gifHeight)
CGImageSourceRef gifSource = CGImageSourceCreateWithURL(url, NULL);
// get frame count
size_t frameCount = CGImageSourceGetCount(gifSource);
for (size_t i = 0; i & frameC ++i) {
// get each frame
CGImageRef frame = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
[frames addObject:(id)frame];
CGImageRelease(frame);
// get gif info with each frame
NSDictionary *dict = (NSDictionary*)CGImageSourceCopyPropertiesAtIndex(gifSource, i, NULL);
NSLog(@&kCGImagePropertyGIFDictionary %@&, [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary]);
// get gif size
if (gifWidth != NULL && gifHeight != NULL) {
*gifWidth = [[dict valueForKey:(NSString*)kCGImagePropertyPixelWidth] floatValue];
*gifHeight = [[dict valueForKey:(NSString*)kCGImagePropertyPixelHeight] floatValue];
// kCGImagePropertyGIFDictionary中kCGImagePropertyGIFDelayTime,kCGImagePropertyGIFUnclampedDelayTime值是一样的
NSDictionary *gifDict = [dict valueForKey:(NSString*)kCGImagePropertyGIFDictionary];
[delayTimes addObject:[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime]];
if (totalTime) {
*totalTime = *totalTime + [[gifDict valueForKey:(NSString*)kCGImagePropertyGIFDelayTime] floatValue];
@interface SvGifView() {
NSMutableArray *_
NSMutableArray *_frameDelayT
CGFloat _totalT // seconds
@implementation SvGifView
- (id)initWithCenter:(CGPoint)center fileURL:(NSURL*)fileURL;
self = [super initWithFrame:CGRectZero];
if (self) {
_frames = [[NSMutableArray alloc] init];
_frameDelayTimes = [[NSMutableArray alloc] init];
_width = 0;
_height = 0;
if (fileURL) {
getFrameInfo((CFURLRef)fileURL, _frames, _frameDelayTimes, &_totalTime, &_width, &_height);
self.frame = CGRectMake(0, 0, _width, _height);
self.center =
+ (NSArray*)framesInGif:(NSURL *)fileURL
NSMutableArray *frames = [NSMutableArray arrayWithCapacity:3];
NSMutableArray *delays = [NSMutableArray arrayWithCapacity:3];
getFrameInfo((CFURLRef)fileURL, frames, delays, NULL, NULL, NULL);
- (void)startGif
CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@&contents&];
NSMutableArray *times = [NSMutableArray arrayWithCapacity:3];
CGFloat currentTime = 0;
int count = _frameDelayTimes.
for (int i = 0; i & ++i) {
[times addObject:[NSNumber numberWithFloat:(currentTime / _totalTime)]];
currentTime += [[_frameDelayTimes objectAtIndex:i] floatValue];
[animation setKeyTimes:times];
NSMutableArray *images = [NSMutableArray arrayWithCapacity:3];
for (int i = 0; i & ++i) {
[images addObject:[_frames objectAtIndex:i]];
[animation setValues:images];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear]];
animation.duration = _totalT
animation.delegate =
animation.repeatCount = 5;
[self.layer addAnimation:animation forKey:@&gifAnimation&];
- (void)stopGif
[self.layer removeAllAnimations];
// remove contents when animation end
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
self.layer.contents =
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
// Drawing code
代码很短也比较容易,就不一一解释了。最开始的那个C函数主要就是用来解析Gif的,之所以用C函数是因为我要返回多个信息,而Objective-c只能返回一个参数,而且Objective-c和C语言可以很方便的混合编程。
另外再介绍两种使用UIImageView的方法:
1. 使用UIWebView播放
// 设定位置和大小
CGRect frame = CGRectMake(50,50,0,0);
frame.size = [UIImage imageNamed:@&guzhang.gif&].
// 读取gif图片数据
NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@&guzhang& ofType:@&gif&]];
// view生成
UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
webView.userInteractionEnabled = NO;//用户不可交互
[webView loadData:gif MIMEType:@&image/gif& textEncodingName:nil baseURL:nil];
[self.view addSubview:webView];
[webView release];
2.将gif图片分解成多张png图片,使用UIImageView播放。
代码如下:
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@&1&],
[UIImage imageNamed:@&2&],
[UIImage imageNamed:@&3&],
[UIImage imageNamed:@&4&],
[UIImage imageNamed:@&5&],
[UIImage imageNamed:@&6&],
[UIImage imageNamed:@&7&],
[UIImage imageNamed:@&8&],
[UIImage imageNamed:@&9&],
[UIImage imageNamed:@&10&],
[UIImage imageNamed:@&11&],
[UIImage imageNamed:@&12&],
[UIImage imageNamed:@&13&],
[UIImage imageNamed:@&14&],
[UIImage imageNamed:@&15&],
[UIImage imageNamed:@&16&],
[UIImage imageNamed:@&17&],
[UIImage imageNamed:@&18&],
[UIImage imageNamed:@&19&],
[UIImage imageNamed:@&20&],
[UIImage imageNamed:@&21&],
[UIImage imageNamed:@&22&],nil];
gifImageView.animationImages = gifA //动画图片数组
gifImageView.animationDuration = 5; //执行一次完整动画所需的时长
gifImageView.animationRepeatCount = 1; //动画重复次数
[gifImageView startAnimating];
[self.view addSubview:gifImageView];
[gifImageView release];
注意:这个方法,如果gif动画每桢间的时间间隔不同,不能达到此效果。
大家还看了:
[访问统计:]
上一篇:下一篇:iOS之加载Gif图片 - 简书
iOS之加载Gif图片
Gif图片是非常常见的图片格式,尤其是在聊天的过程中,Gif表情使用地很频繁。但是iOS竟然没有现成的支持加载和播放Gif的类。
简单地汇总了一下,大概有以下几种方法:
一、加载本地Gif文件
1、使用UIWebView
// 读取gif图片数据
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,200,200)];
[self.view addSubview:webView];
NSString *path = [[NSBundle mainBundle] pathForResource:@"001" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
使用loadData:MIMEType:textEncodingName: 则有警告
[webView loadData:data MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
NSURL *url = [NSURL URLWithString:path];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
但是使用UIWebView的弊端在于,不能设置Gif动画的播放时间。
2、将Gif拆分成多张图片,使用UIImageView播放
最好把所需要的Gif图片打包到Bundle文件内,如下图所示
Loading.png
- (NSArray *)animationImages
NSFileManager *fielM = [NSFileManager defaultManager];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Loading" ofType:@"bundle"];
NSArray *arrays = [fielM contentsOfDirectoryAtPath:path error:nil];
NSMutableArray *imagesArr = [NSMutableArray array];
for (NSString *name in arrays) {
UIImage *image = [UIImage imageNamed:[(@"Loading.bundle") stringByAppendingPathComponent:name]];
if (image) {
[imagesArr addObject:image];
return imagesA
- (void)viewDidLoad {
[super viewDidLoad];
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:frame];
gifImageView.animationImages = [self animationImages]; //获取Gif图片列表
gifImageView.animationDuration = 5;
//执行一次完整动画所需的时长
gifImageView.animationRepeatCount = 0;
//动画重复次数
[gifImageView startAnimating];
[self.view addSubview:gifImageView];
3、使用SDWebImage
但是很遗憾,SDWebImage 的 sd_setImageWithURL:placeholderImage:这个方法是不能播放本地Gif的,它只能显示Gif的第一张图片而已。So,此方法行不通
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:frame];
[gifImageView sd_setImageWithURL:nil placeholderImage:[UIImage imageNamed:@"gifTest.gif"]];
其实,在SDWebImage这个库里有一个UIImage+GIF的类别,里面为UIImage扩展了三个方法:
@interface UIImage (GIF)
+ (IImage *)sd_animatedGIFNamed:(NSString *)
+ (UIImage *)sd_animatedGIFWithData:(NSData *)
- (UIImage *)sd_animatedImageByScalingAndCroppingToSize:(CGSize)
大家一看就知道,我们要获取处理后的Gif图片,其实只要调用前面两个中的其中一个方法就行了
注意:第一个只需要传Gif的名字,而不需要带扩展名(如Gif图片名字为001@2x.gif,只需传001即可)
我们就使用第二个方法试一试效果:
NSString *path = [[NSBundle mainBundle] pathForResource:@"gifTest" ofType:@"gif"];
NSData *data = [NSData dataWithContentsOfFile:path];
UIImage *image = [UIImage sd_animatedGIFWithData:data];
gifImageView.image =
然后通过断点,我们看下获取到的image是个什么样的东东:
我们发现:
image的isa指针指向了_UIAnimatedImage ,说明它是一个叫作_UIAnimatedImage 的类(当然,这个_UIAnimatedImage 苹果是不会直接让我们使用的)
_images 表示:这个Gif包含了多少张图片
_duration表示:执行一次完整动画所需的时长
其实,动画执续时间_duration也可以更改!
我们来看下此方法的内部实现:
+ (UIImage *)sd_animatedGIFWithData:(NSData *)data {
if (!data) {
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
size_t count = CGImageSourceGetCount(source);
UIImage *animatedI
if (count &= 1) {
animatedImage = [[UIImage alloc] initWithData:data];
NSMutableArray *images = [NSMutableArray array];
NSTimeInterval duration = 0.0f;
for (size_t i = 0; i & i++) {
CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
duration += [self sd_frameDurationAtIndex:i source:source];
[images addObject:[UIImage imageWithCGImage:image scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp]];
CGImageRelease(image);
if (!duration) {
duration = (1.0f / 10.0f) *
animatedImage = [UIImage animatedImageWithImages:images duration:duration];
CFRelease(source);
return animatedI
很明显,duration是可以随意更改的,只不过此方法设置了一个默认值
(duration = (1.0f / 10.0f) * count)
归根到底,创建新的动态的Image其实是调用了系统提供的一个UIImage的类方法而已:
UIImage *animatedImage = [UIImage animatedImageWithImages:images duration:duration];
二、加载网络Gif文件
加载网络的Gif文件就简单多了。最简单的方法,我们只需要使用SDWebImage 的 sd_setImageWithURL:这个方法传入Gif文件是url地址即可。
纠其原因:稍微仔细看了SDWebImage内部实现就可以清楚,大概是以下几个步骤:
1、SDWebImage根据url将Gif文件下载下来,格式为一个NSData
2、如果判断是Gif格式,则会调用** sd_animatedGIFWithData:** 将Data转换成我们需要的Gif格式
3、通过上面的方法二即可显示出Gif图片
UIImage *image = [UIImage sd_animatedGIFWithData:data];
gifImageView.image =
............................................................
一、加载本地Gif文件
1、使用UIWebView不可以设置duration,其他两种方法都可设置。而且方法1的容器为UIWebView ,其余两种的容器都是大家熟悉的UIImageView
2、方法2和方法3需要对应看应用场景
如:下拉、上拉加载控件需要一个根据拉动距离设置特定的Image,则需要使用方法2
直接显示Gif图片,则使用方法3会更方便
二、加载网络Gif文件
直接使用SDWebImage 的 sd_setImageWithURL:这个方法传入Gif文件是url地址即可
............................................................

我要回帖

更多关于 word添加gif动画 的文章

 

随机推荐