天天看點

常見的gif動畫加載

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]; 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];