iOS 直接載入gif動畫

來源:互聯網
上載者:User

標籤:ios開發   動畫   圖片   

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


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

iOS 直接載入gif動畫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.