加载一张gif动态图
2016-05-25 11:46
211 查看
不知道大家有没有碰到过加载一张gif图的问题 这里我整理了两个方法第一个是通过html 加载到一个Web上 另一个是通过SDWebImage来实现的 其实两个原理都是差不多的 使用第三方可能比较简单 但是大家可以可以自己写一下,话不多说上代码:
第一种方法:
[super.navigationController setNavigationBarHidden:YES animated:TRUE];
webView = [[UIWebView alloc]init];
webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
// 设定位置和大小
NSString *html = [NSString stringWithFormat:@"<html><head><style>body{margin:0px;padding:0px}</style></head><body><img
border=\"0\" src = \"searchwait.gif\" width=\"%f\"height=\"%f\" style=\"position: absolute; z-index: -1;margin:0px;padding:0px\"><body></head></html>",self.view.bounds.size.width,self.view.bounds.size.height];
NSString *path = [[NSBundle mainBundle] pathForResource:@"searchwait"ofType:@"gif"];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:html baseURL:baseURL];
webView.userInteractionEnabled = NO;
webView.backgroundColor = wc_white;
webView.contentMode = UIViewContentModeScaleAspectFill;
webView.clipsToBounds = YES;
[self.view addSubview:webView];
第二种方法:
- (void)initLoadingImageView
{
NSString *name =@"loading.gif";
NSString *filePath = [[NSBundlebundleWithPath:[[NSBundlemainBundle]
bundlePath]]pathForResource:name
ofType:nil];
NSData *imageData = [NSDatadataWithContentsOfFile:filePath];
if (!self.loadingImageView) {
self.loadingImageView = [[UIImageView alloc]init];
}
// imgView.image = [UIImage animatedImageWithAnimatedGIFData:imageData];
self.loadingImageView.backgroundColor = [UIColor clearColor];
self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];
self.loadingImageView.frame = CGRectMake(0,0,
100,250/2.0);
[self.view addSubview:self.loadingImageView];
[self.view bringSubviewToFront:self.loadingImageView];
}
第一种方法:
[super.navigationController setNavigationBarHidden:YES animated:TRUE];
webView = [[UIWebView alloc]init];
webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
// 设定位置和大小
NSString *html = [NSString stringWithFormat:@"<html><head><style>body{margin:0px;padding:0px}</style></head><body><img
border=\"0\" src = \"searchwait.gif\" width=\"%f\"height=\"%f\" style=\"position: absolute; z-index: -1;margin:0px;padding:0px\"><body></head></html>",self.view.bounds.size.width,self.view.bounds.size.height];
NSString *path = [[NSBundle mainBundle] pathForResource:@"searchwait"ofType:@"gif"];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:html baseURL:baseURL];
webView.userInteractionEnabled = NO;
webView.backgroundColor = wc_white;
webView.contentMode = UIViewContentModeScaleAspectFill;
webView.clipsToBounds = YES;
[self.view addSubview:webView];
第二种方法:
- (void)initLoadingImageView
{
NSString *name =@"loading.gif";
NSString *filePath = [[NSBundlebundleWithPath:[[NSBundlemainBundle]
bundlePath]]pathForResource:name
ofType:nil];
NSData *imageData = [NSDatadataWithContentsOfFile:filePath];
if (!self.loadingImageView) {
self.loadingImageView = [[UIImageView alloc]init];
}
// imgView.image = [UIImage animatedImageWithAnimatedGIFData:imageData];
self.loadingImageView.backgroundColor = [UIColor clearColor];
self.loadingImageView.image = [UIImage sd_animatedGIFWithData:imageData];
self.loadingImageView.frame = CGRectMake(0,0,
100,250/2.0);
[self.view addSubview:self.loadingImageView];
[self.view bringSubviewToFront:self.loadingImageView];
}
相关文章推荐
- LeetCode 040 Combination Sum II
- 栈的链表实现(C语言)
- 如何解决日志文件过大问题
- ClassCastException Log4jLoggerFactory LoggerContex
- 由于权限不足而无法读取配置文件出现的HTTP 500.19解决办法
- [c++]Xcode配置gtest
- RxJava学习小结
- LINUX 内核算杂 七杂 八
- BBC-Present Simple and Present Continuous
- Mybatis事务管理
- [置顶] Qt5.5.0编译QtAV
- [置顶] YJCocoa Pod开源库
- 简约至上读书笔记
- html5使用小技巧(永久更新,多为转载)
- Lliux单磁盘根分区扩容(非LVM)
- Hadoop可视化与交互式工具:Zeppelin和Hue
- Oracle函数--字符串拼接
- 101. Symmetric Tree
- Xml背景
- LeetCode 039 Combination Sum