在開發(fā)iOS程序的過程中斟叼,可能會(huì)遇到要加載gif圖片,下面介紹gif圖片的加載方式:
先介紹一個(gè)第三方:gifView
在導(dǎo)入gifView后春寿,由于gifView是在MRC下的朗涩,所以需要配置工程
QQ20160511-0@2x.png
之后就是在工程中書寫代碼的過程了。
#import "ViewController.h"
#import "GifView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//第一種 用UIImageView中的動(dòng)畫數(shù)組播放動(dòng)畫
// [self showGifImageMethodOne];
//第二種 用UIWebView顯示
// [self showGifImageMethodTwo];
//第三種 用第三方GifView顯示本地圖片
[self showGifImageMethodThree];
//用第三方顯示從網(wǎng)絡(luò)獲取的動(dòng)態(tài)圖片
[self showGifImageMethodFour];
}
#pragma mark 播放動(dòng)態(tài)圖片方式1 UIImageView
-(void)showGifImageMethodOne
{
//第一種 用UIImageView中的動(dòng)畫數(shù)組播放動(dòng)畫
//創(chuàng)建UIImageView绑改,添加到界面
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 100, 100)];
[self.view addSubview:imageView];
//創(chuàng)建一個(gè)數(shù)組谢床,數(shù)組中按順序添加要播放的圖片(圖片為靜態(tài)的圖片)
NSMutableArray *imgArray = [NSMutableArray array];
for (int i=1; i<7; i++) {
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"clock%02d.png",i]];
[imgArray addObject:image];
}
//把存有UIImage的數(shù)組賦給動(dòng)畫圖片數(shù)組
imageView.animationImages = imgArray;
//設(shè)置執(zhí)行一次完整動(dòng)畫的時(shí)長
imageView.animationDuration = 6*0.15;
//動(dòng)畫重復(fù)次數(shù) (0為重復(fù)播放)
imageView.animationRepeatCount = 0;
//開始播放動(dòng)畫
[imageView startAnimating];
//停止播放動(dòng)畫 - (void)stopAnimating;
//判斷是否正在執(zhí)行動(dòng)畫 - (BOOL)isAnimating;
}
#pragma mark 播放動(dòng)態(tài)圖片方式2 UIWebView
-(void)showGifImageMethodTwo
{
//第二種 用UIWebView顯示
//得到圖片的路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"happy" ofType:@"gif"];
//將圖片轉(zhuǎn)為NSData
NSData *gifData = [NSData dataWithContentsOfFile:path];
//創(chuàng)建一個(gè)webView,添加到界面
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 150, 200, 200)];
[self.view addSubview:webView];
//自動(dòng)調(diào)整尺寸
webView.scalesPageToFit = YES;
//禁止?jié)L動(dòng)
webView.scrollView.scrollEnabled = NO;
//設(shè)置透明效果
webView.backgroundColor = [UIColor clearColor];
webView.opaque = 0;
//加載數(shù)據(jù)
[webView loadData:gifData MIMEType:@"image/gif" textEncodingName:nil baseURL:nil];
}
#pragma mark 播放動(dòng)態(tài)圖片方式3 第三方顯示本地動(dòng)態(tài)圖片
-(void)showGifImageMethodThree
{
//方式一
//將圖片轉(zhuǎn)為NSData數(shù)據(jù)
NSData *localData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"songshu" ofType:@"gif"]];
//創(chuàng)建一個(gè)第三方的View顯示圖片
GifView *dataView = [[GifView alloc] initWithFrame:CGRectMake(0, 300, 200, 100) data:localData];
[self.view addSubview:dataView];
//方式二
//得到圖片的路徑
NSString *path = [[NSBundle mainBundle] pathForResource:@"songshu" ofType:@"gif"];
GifView *dataView2 = [[GifView alloc] initWithFrame:CGRectMake(200, 300, 150, 100) filePath:path];
[self.view addSubview:dataView2];
}
#pragma mark 播放動(dòng)態(tài)圖片方式3 第三方顯示從網(wǎng)絡(luò)獲取的動(dòng)態(tài)圖片
-(void)showGifImageMethodFour
{
// 網(wǎng)絡(luò)圖片
NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://pic19.nipic.com/20120222/8072717_124734762000_2.gif"]];
//創(chuàng)建一個(gè)第三方的View顯示圖片
GifView *dataViewWeb = [[GifView alloc] initWithFrame:CGRectMake(20, 420, 280, 100) data:urlData];
[self.view addSubview:dataViewWeb];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end