IOS瘋狂基礎之GIF圖片的顯示

IOS瘋狂基礎之GIF圖片的顯示 web

 

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"],
                                                  [UIImage imageNamed:@"10"],
                                                  [UIImage imageNamed:@"11"],
                                                  [UIImage imageNamed:@"12"],
                                                  [UIImage imageNamed:@"13"],
                                                  [UIImage imageNamed:@"14"],
                                                  [UIImage imageNamed:@"15"],
                                                  [UIImage imageNamed:@"16"],
                                                  [UIImage imageNamed:@"17"],
                                                  [UIImage imageNamed:@"18"],
                                                  [UIImage imageNamed:@"19"],
                                                  [UIImage imageNamed:@"20"],
                                                  [UIImage imageNamed:@"21"],
                                                  [UIImage imageNamed:@"22"],nil];
    gifImageView.animationImages = gifArray; //動畫圖片數組
    gifImageView.animationDuration = 5; //執行一次完整動畫所需的時長
    gifImageView.animationRepeatCount = 1;  //動畫重複次數
    [gifImageView startAnimating];
    [self.view addSubview:gifImageView];
    [gifImageView release];

注意:這個方法,若是gif動畫每楨間的時間間隔不一樣,不能達到此效果。數組

相關文章
相關標籤/搜索