iOS 直接加载gif动画

<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">
 


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值