標籤: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動畫