ios中播放gif動畫

iPhone SDK提供了多種動畫手段,UIView、UIImageView和CALayer都支持動畫。但如何處理常見的gif動畫呢?UIWebView提供了答案,代碼如下:

1. 使用UIWebView播放
    // 設定位置和大小
    CGRect frame = CGRectMake(50,50,0,0);
    frame.size = [UIImage imageNamed:@"guzhang.gif"].size;
    // 讀取gif圖片數據
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"guzhang" ofType:@"gif"]];
    // view生成
    UIWebView *webView = [[UIWebView alloc] initWithFrame:frame];
    webView.userInteractionEnabled = NO;//用戶不可交互
    [webView loadData:gif MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
    [self.view addSubview:webView];
    [webView release];


2. 將gif圖片分解成多張png圖片,使用UIImageView播放。
代碼如下:
UIImageView *gifImageView = [[UIImageView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    NSArray *gifArray = [NSArray arrayWithObjects:[UIImage imageNamed:@"1"],
                                                  [UIImage imageNamed:@"2"],
                                                  [UIImage imageNamed:@"3"],
                                                  [UIImage imageNamed:@"4"],
                                                  [UIImage imageNamed:@"5"],
                                                  [UIImage imageNamed:@"6"],
                                                  [UIImage imageNamed:@"7"],
                                                  [UIImage imageNamed:@"8"],
                                                  [UIImage imageNamed:@"9"],nil];
    gifImageView.animationImages = gifArray; //動畫圖片數組
    gifImageView.animationDuration = 5; //執行一次完整動畫所需的時長
    gifImageView.animationRepeatCount = 1;  //動畫重複次數
    [gifImageView startAnimating];
    [self.view addSubview:gifImageView];
    [gifImageView release]; 


3. 本次實現gif動畫播放是通過將動畫文件讀取到CGImageSourceRef,然後用NSTimer來播放的。

代碼如下:

首先是頭文件

#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface GifView : UIView 
{
CGImageSourceRef gif; // 保存gif動畫
NSDictionary *gifProperties; // 保存gif動畫屬性
size_t index; // gif動畫播放開始的幀序號
size_t count; // gif動畫的總幀數
NSTimer *timer; // 播放gif動畫所使用的timer
}

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath;- (void)stopGif;

@end


接下來是實現

 
#import "GifView.h"
#import <QuartzCore/QuartzCore.h>

@implementation GifView

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath
{
   self = [super initWithFrame:frame];
   if (self)
   {
       NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
   
       gifProperties = [[NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary] retain];
       
       gif = CGImageSourceCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path], (CFDictionaryRef)gifProperties);
       
       count =CGImageSourceGetCount(gif);

       timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(play) userInfo:nil repeats:YES];
       [timer fire];
   }
   return self;
}

-(void)play
{
   index ++;
   index = index%count;
   CGImageRef ref = CGImageSourceCreateImageAtIndex(gif, index, (CFDictionaryRef)gifProperties);
   self.layer.contents = (id)ref;
   CFRelease(ref);
}

- (void)dealloc
{
   NSLog(@"dealloc");
   CFRelease(gif);
   [gifProperties release];
   [super dealloc];
}

- (void)stopGif
{
   [timer invalidate];
   timer = nil;
}

@end


這個類比較簡單,在方法

- (id)initWithFrame:(CGRect)frame filePath:(NSString *)filePath

調用結束就開始播放動畫,如果需要用戶指定何時播放的話,只需要把timer的開始放到合適的位置。通過對CFDictonaryRaf 也就是gifProperties的改變,我們還可以控制動畫是否循環播放以及循環多少次停止。

通過對index的改變也可以控制動畫從某幀開始播放。同理,同時改變index和count的話,也可以控制從某幀到某幀的播放。

 

注:必須調用方法

- (void)stopGif

之後纔可以退出這個類。否則timer不會關閉,產生內存泄露。

另注:需要QuartzCore.framework;MobileCoreServices.framework;ImageIO.framework 這三個framework的支持


4. SCGIFImageView是一個開源的GIF圖片動畫顯示控件,通過將GIF的每一幀都取出來生成UIImage對象存放在一個數組中,然後使用NSTimer進行動畫輪轉。

示例代碼:

NSString* filePath = [[NSBundle mainBundle] pathForResource:@"1.gif" ofType:nil];
NSData* imageData = [NSData dataWithContentsOfFile:filePath];

SCGIFImageView* gifImageView = [[[SCGIFImageView alloc] initWithFrame:self.view.bounds] autorelease];
[gifImageView setData:imageData];

[self.view addSubview:gifImageView];


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章