关闭

iOS 直接加载gif动画

标签: ios开发动画图片
1118人阅读 评论(0) 收藏 举报
分类:
<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
0

猜你在找
【直播】计算机视觉原理及实战——屈教授
【套餐】深度学习入门视频课程——唐宇迪
【套餐】Hadoop生态系统零基础入门
【套餐】嵌入式Linux C编程基础
【套餐】2017软考系统集成项目——任铄
【套餐】Android 5.x顶级视频课程——李宁
【直播】广义线性模型及其应用——李科
【直播】从0到1 区块链的概念到实践
【直播】机器学习之凸优化——马博士
【套餐】微信订阅号+服务号Java版 v2.0--翟东平
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10524次
    • 积分:275
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:1篇
    • 译文:1篇
    • 评论:1条
    最新评论