<pre name="code" class="objc">// iOS 加载gif动画,不用一帧帧切图,直接实现加载.gif图片
</pre><pre name="code" class="objc">// 首先我们定义了一个<span style="font-family: Arial, Helvetica, sans-serif;">NTVGifAnimationView类,来加载</span>
#import <UIKit/UIKit.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import <ImageIO/ImageIO.h>
#define Gif_W 114
#define Lab_H 30
#define Gif_H (114+Lab_H)
@interface NTVGifAnimationView : UIView
{
CGImageSourceRef _gif; // 保存gif动画
NSDictionary *_gifProperties; // 保存gif动画属性
size_t _index; // gif动画播放开始的帧序号
size_t _count; // gif动画的总帧数
NSTimer *_timer; // 播放gif动画所使用的timer
UIView *_gifView;
UILabel *_label;
}
</pre><pre name="code" class="objc"><p class="p1"><span class="s1">/**</span></p><p class="p2"><span class="s2"> * 初始化方法</span></p><p class="p1"><span class="s1"> *</span></p><p class="p1"><span class="s1"> * @param frame </span><span style="font-family: Arial, Helvetica, sans-serif;">NTVGifAnimationView 对象的</span><span style="font-family: Arial, Helvetica, sans-serif;">frame</span></p><p class="p1"><span class="s1"> * @param <span style="font-family: Arial, Helvetica, sans-serif;">filePath gif图片地址</span></span></p><p class="p1"><span class="s1"> *</span></p><p class="p1"><span class="s1"> */</span></p>
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath;
- (void) playGif; // 播放gif动画
- (void)stopGif; // 停止gif动画
@end
</pre><br /><pre name="code" class="objc">#import "NTVGifAnimationView.h"
#import <QuartzCore/QuartzCore.h>
@implementation NTVGifAnimationView
- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath
{
self = [super initWithFrame:frame];
if (self)
{
CGRect rect = frame;
_gifView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, rect.size.width, rect.size.height - Lab_H)];
_gifView.backgroundColor = [UIColor clearColor];
[self addSubview:_gifView];
_label = [[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_gifView.frame), rect.size.width, Lab_H)];
_label.textAlignment = NSTextAlignmentCenter;
_label.backgroundColor = [UIColor clearColor];
_label.text = @"加载中...";
[self addSubview:_label];
NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
_gifProperties = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary];
_gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:filePath], (CFDictionaryRef)_gifProperties);
_count =CGImageSourceGetCount(_gif);
}
return self;
}
-(void)play
{
_index ++;
_index = _index%_count;
CGImageRef ref = CGImageSourceCreateImageAtIndex(_gif, _index, (CFDictionaryRef)_gifProperties);
_gifView.layer.contents = (__bridge id)ref;
CFRelease(ref);
}
- (void)dealloc
{
DLog(@"dealloc");
CFRelease(_gif);
}
- (void) playGif
{
_timer = [NSTimer scheduledTimerWithTimeInterval:0.03 target:self selector:@selector(play) userInfo:nil repeats:YES];
[_timer fire];
}
- (void)stopGif
{
[_timer invalidate];
_timer = nil;
[self removeFromSuperview];
}
@end
</pre><pre name="code" class="objc">