您的位置:首页 > 移动开发 > IOS开发

iOS显示GIF图片(3中方法)

2015-12-25 17:36 453 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。

先倒入GifView.h、GifView.m 类

#import "ViewController.h"

#import "GifView.h"

@interface
ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//1.
第三方

//
网络图片

// NSData *urlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.chinagif.com/gif/part/boy/0045.gif"]];

//
本地图片

NSData *localData = [NSDatadataWithContentsOfFile:[[NSBundlemainBundle]
pathForResource:@"run"ofType:@"gif"]];

GifView *dataView = [[GifViewalloc]
initWithFrame:CGRectMake(0,0,
100, 100)
data:localData];
[self.viewaddSubview:dataView];
[dataViewrelease];

//
或者

GifView *pathView =[[GifViewalloc]
initWithFrame:CGRectMake(100,0,
100,100)
filePath:[[NSBundlemainBundle]
pathForResource:@"run"ofType:@"gif"]];
[self.viewaddSubview:pathView];
[pathViewrelease];

//2. webView

NSString *path = [[NSBundlemainBundle]
pathForResource:@"run"ofType:@"gif"];
NSData *gifData = [NSDatadataWithContentsOfFile:path];
UIWebView *webView = [[UIWebViewalloc]
initWithFrame:CGRectMake(0,120,
100, 100)];

webView.backgroundColor = [UIColorredColor];
webView.scalesPageToFit =YES;

[webView loadData:gifDataMIMEType:@"image/gif"textEncodingName:nilbaseURL:nil];
[self.viewaddSubview:webView];
[webViewrelease];

//3. animationView

UIImageView *gifImageView = [[UIImageViewalloc]
initWithFrame:CGRectMake(0,240,
100, 100)];

NSArray *gifArray = [NSArrayarrayWithObjects:[UIImageimageNamed:@"1"],
[UIImageimageNamed:@"2"],
[UIImageimageNamed:@"3"],
[UIImageimageNamed:@"4"],
[UIImageimageNamed:@"5"],
[UIImageimageNamed:@"6"],
[UIImageimageNamed:@"7"],
[UIImageimageNamed:@"8"],
[UIImageimageNamed:@"9"],
[UIImageimageNamed:@"10"],
[UIImageimageNamed:@"11"],
[UIImageimageNamed:@"12"],
[UIImageimageNamed:@"13"],
[UIImageimageNamed:@"14"],
[UIImageimageNamed:@"15"],
[UIImageimageNamed:@"16"],
[UIImageimageNamed:@"17"],
[UIImageimageNamed:@"18"],
[UIImageimageNamed:@"19"],
[UIImageimageNamed:@"20"],
[UIImageimageNamed:@"21"],
[UIImageimageNamed:@"22"],nil];
gifImageView.animationImages = gifArray;//动画图片数组
gifImageView.animationDuration =5;
//执行一次完整动画所需的时长
gifImageView.animationRepeatCount =999;
//动画重复次数
[gifImageViewstartAnimating];
[self.viewaddSubview:gifImageView];
[gifImageViewrelease];
}

- (void)viewDidUnload
{

[superviewDidUnload];

// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{

return (interfaceOrientation !=UIInterfaceOrientationPortraitUpsideDown);
}

@end

转载请注明出处:http://blog.csdn.net/sevenquan
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: