UIImageView异步加载网络图片
在iOS开发过程中,经常会遇到使用UIImageView展现来自网络的图片的情况,最简单的做法如下:
去下载https://github.com/rs/SDWebImage放进你的工程里,加入头文件#import "UIImageView+WebCache.h"
加载网路图片就一句话[imageView setImageWithURL:url placeholderImage:[UIImage imageNamed:@"defaultImage.png"]];
imageView为要显示的UIImageView,url为图片url地址(如果是NSString,就转换成NSUrl--[NSURL URLWithString:string]),
defaultImage.png为占位图片!
另一篇:
共享一个自己写的动态加载网络图片并自动剪裁的类
相关类库,小弟不才基于EGOImageView自己写了一个私有协议,自己感觉效果不错,之前在论坛一直用别人的代码,今天也共享下。
有需要的朋友可以看看,可能不是最好,有问题请多多指教。
AutoCutPicSimple_New.zip做了点修改,防止多次seturl导致crash。
另一篇:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
- self.imageView.layer.masksToBounds = YES;
- self.imageView.layer.cornerRadius = 5.0f;
- [self.imageView setBackgroundColor:[UIColor grayColor]];
- [self.view addSubview:self.imageView];
- NSURL *imageUrl = [NSURL URLWithString:IMAGE_URL];
- UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
- self.imageView.image = image;
- }
这么做,最直接的问题就是阻塞UI线程了。
于是考虑利用NSOperationQueue来异步加载图片:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- operationQueue = [[NSOperationQueue alloc] init];
- self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)] autorelease];
- self.imageView.layer.masksToBounds = YES;
- self.imageView.layer.cornerRadius = 5.0f;
- [self.imageView setBackgroundColor:[UIColor grayColor]];
- [self.view addSubview:self.imageView];
- NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
- [operationQueue addOperation:op];
- }
- - (void)downloadImage
- {
- NSURL *imageUrl = [NSURL URLWithString:IMAGE_URL];
- UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
- self.imageView.image = image;
- }
这么做的话,就可以避免阻塞UI线程了。当图片异步加载完成后,就会展现出来。
但是,第二次进入该界面,还是要重新下载图片,用户体验不好,且浪费资源(比如耗电)。
于是,考虑缓存已经下载的图片。
模仿操作系统(Cache - Memory - Disk),缓存图片也可以采取两层模型:内存和磁盘。
保存到内存中比较简单,只需要用NSDictionary来维护即可。而保存到磁盘,涉及到本地文件读写,可以参考“文件和数据管理”。
首先需要创建一个缓存目录:
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
- if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) {
- NSError *error = nil;
- [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath
- withIntermediateDirectories:YES
- attributes:nil
- error:&error];
- }
接着可以用图片名称或者URL或者hash过后的值作为key(本地文件名),写入到本地:
- if (![[NSFileManager defaultManager] fileExistsAtPath:localPath]) {
- [[NSFileManager defaultManager] createFileAtPath:localPath contents:localData attributes:nil];
- }
这样,在每次下载图片之前,先判断是否已经有缓存了,可以优化体验和性能。
我把完整的源代码写成Category以重用,放在GitHub上:https://github.com/siqin/OnlineImageView 。
Jason Lee @ Hangzhou
Blog: http://blog.csdn.net/jasonblog
第二篇:
方法1:在UI线程中同步加载网络图片
- UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
- NSURL *photourl = [NSURL URLWithString:@"http://www.exampleforphoto.com/pabb/test32.png"];
- //url请求实在UI主线程中进行的
- UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
- headview.image = images;
这是最简单的,但是由于在主线程中加载,会阻塞UI主线程。所以可以试试NSOperationQueue,一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。
------------------------------------------------------------------------------------------------
方法2:使用NSOperationQueue异步加载
- 下面就是使用NSOperationQueue实现子线程加载图片:
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
- self.imageview = [[[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)] autorelease];
- [self.imageview setBackgroundColor:[UIColor grayColor]];
- [self.view addSubview:self.imageview];
- NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
- [operationQueue addOperation:op];
- }
- - (void)downloadImage
- {
- NSURL *imageUrl = [NSURL URLWithString:HEADIMAGE_URL];
- UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
- self.imageview.image = image;
- }
不过这这样的设计,虽然是异步加载,但是没有缓存图片。重新加载时又要重新从网络读取图片,重复请求,实在不科学,所以可以考虑第一次请求时保存图片。
------------------------------------------------------------------------------------------------
方法3:异步加载,保存到cache里,下次再请求时读取cache里的缓存图片
3-1,首先在http请求时,建立一个缓存目录
- NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSLog(@"the paths:%@",paths);
- NSString * diskCachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"imageCache"];
- NSLog(@"diskCachePath:%@",diskCachePath);
- //如果目录imageCache不存在,创建目录
- if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) {
- NSError *error=nil;
- [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath withIntermediateDirectories:YES attributes:nil error:&error];
- }
3-2创建好目录后,第一次时,url请求网络图片,然后把得到的data保存到本地cache里。
- <span style="white-space:pre"> </span> NSURL *headurl = [NSURL URLWithString:<pre name="code" class="cpp">@"http://hiweibo-common.stor.sinaapp.com/user_photo_34.png"<span style="font-family: Arial, Helvetica, sans-serif;">]; </span></pre>// UIImage *headimg = [UIImage imageWithData:[NSData dataWithContentsOfURL:headurl]]; NSData *imagedata = [NSData dataWithContentsOfURL:headurl]; //如果本地缓存没有,保存图片 NSString *localPath = [NSString stringWithFormat:@"%@/headimage.png",diskCachePath];//
- NSLog(@"localpaht:%@",localPath); if (imagedata) { if (![[NSFileManager defaultManager] fileExistsAtPath:localPath]) { [[NSFileManager defaultManager] createFileAtPath:localPath contents:imagedata attributes:nil]; //[imagedata writeToFile:localPath atomically:YES];//也可以data
- write到file里 } UIImage *headimg = [[UIImage alloc] initWithData:imagedata]; self.headimage = headimg; [headimg release]; [meTableView reloadData]; }<p></p>
- <pre></pre>
- 3-3然后在下一次打开,初始化设置图片的地方,判断cache里有没有缓存图片,有的话直接加载。
- <p></p>
- <p><span style="font-size:18px"></span></p><pre name="code" class="cpp"> NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *localpath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/imageCache/headimage.png"];
- //如果有缓存图片,直接读取cache内的缓存图片
- if ([[NSFileManager defaultManager] fileExistsAtPath:localpath]) {
- NSData *data = [NSData dataWithContentsOfFile:localpath];
- self.headimage = [UIImage imageWithData:data];
- [meTableView reloadData];
- }
- else{
- //如果没有缓存图片,请求
- NSMutableDictionary *headdict =[NSMutableDictionary dictionaryWithObjectsAndKeys:tokenString,@"token",uidString,@"userid",nil];
- [self handlerPostFormDataWithParams:headdict withURL:APPROVAL_URL_GETPHOTO];
- }</pre><br>
- 方法3是最科学的,只需要一次读取,以后就可以直接用了。<p></p>
- <p><span style="font-size:18px"><br>
- </span></p>
- <p><span style="font-size:18px">目前总结到这里,以后有更深入研究时再补充</span></p>
- <p><span style="font-size:18px">20130314<br>
- <br>
- <br>
- </span><br>
- </p>
UIImageView异步加载网络图片的更多相关文章
- ios UIImageView异步加载网络图片
方法1:在UI线程中同步加载网络图片 UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 4 ...
- ios UIImageView异步加载网络图片2
//1. NSData dataWithContentsOfURL // [self.icon setImage:[UIImage imageWithData:[NSData dataWithCont ...
- android官方开源的高性能异步加载网络图片的Gridview例子
这个是我在安卓安卓巴士上看到的资料,放到这儿共享下.这个例子android官方提供的,其中讲解了如何异步加载网络图片,以及在gridview中高效率的显示图片此代码很好的解决了加载大量图片时,报OOM ...
- Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边
Libgdx实现异步加载网络图片并保存到SD卡或者data/data目录下边,当本地有图片的时候,直接从本地读取图片,如果本地没有图片,将从服务器异步加载图片 package com.example. ...
- wemall app商城源码Android之ListView异步加载网络图片(优化缓存机制)
wemall-mobile是基于WeMall的android app商城,只需要在原商城目录下上传接口文件即可完成服务端的配置,客户端可定制修改.本文分享wemall app商城源码Android之L ...
- Android批量图片加载经典系列——采用二级缓存、异步加载网络图片
一.问题描述 Android应用中经常涉及从网络中加载大量图片,为提升加载速度和效率,减少网络流量都会采用二级缓存和异步加载机制,所谓二级缓存就是通过先从内存中获取.再从文件中获取,最后才会访问网络. ...
- (BUG已修改,最优化)安卓ListView异步加载网络图片与缓存软引用图片,线程池,只加载当前屏之说明
原文:http://blog.csdn.net/java_jh/article/details/20068915 迟点出更新的.这个还有BUG.因为软引应不给力了.2.3之后 前几天的原文有一个线程管 ...
- Android批量图片加载经典系列——使用xutil框架缓存、异步加载网络图片
一.问题描述 为提高图片加载的效率,需要对图片的采用缓存和异步加载策略,编码相对比较复杂,实际上有一些优秀的框架提供了解决方案,比如近期在git上比较活跃的xutil框架 Xutil框架提供了四大模块 ...
- listview中getview异步加载网络图片
前言:本以为异步加载挺简单,因为网上代码多,但真想要做好,还真不那么简单,从看代码到弄懂再到自己写,实在是有太多的东西需要学了,用了两天的时间,终于弄出来了,因为用到回调函数,所以理解起来可能难度有点 ...
随机推荐
- 101个MySQL 的调节和优化的提示
MySQL是一个功能强大的开源数据库.随着越来越多的数据库驱动的应用程序,人们一直在推动MySQL发展到它的极限.这里是101条调节和优化MySQL安装的技巧.一些技巧是针对特定的安装环境的,但这些思 ...
- python中的异常处理
主要用到 try...except...raise...finally... 1. try...except... try: for i in range(1, 1000): print i time ...
- CSS水平居中和垂直居中解决方案
一.CSS 居中 — 水平居中 DIV等标签本身没有定义自己居中的属性,网上很多的方法都是介绍用上级的text-align: center,然后嵌套一层DIV来解决问题. 可是这个方法有时候完全不起作 ...
- spring之初识Ioc&Aop
Spring框架的作用 spring是一个轻量级的企业级框架,提供了ioc容器.Aop实现.dao/orm支持.web集成等功能,目标是使现有的java EE技术更易用,并促进良好的编程习惯. Spr ...
- Android WebView的使用
WebView是View的一个子类,使用它可以在App中嵌入H5页面,可以跟js互相调用. webview有两个方法:setWebChromeClient和setWebClient setWebCli ...
- 使用jvisualvm.exe 的Btrace插件监控应用程序
之前提到使用命令行的方式执行btrace监控,其实jdk提供更好的方式监控应用程序. 我们可以使用jvisualvm.exe加插件的方式监控,这样更加方便. 1.在jvisualvm.exe安装btr ...
- 从客户端中检测到有潜在危险的Request.Form值的解决方法
描述:从客户端中检测到有潜在危险的Request.Form值的解决方法asp.net 2.0 通常解决办法将.aspx文件中的page项添加ValidateRequest="false&qu ...
- 有关C#中使用if else和try catch的问题及效率问题
本来if esle 是流程控制 try catch是异常处理,本身其实是没有可比性的,但是开发过程中有的人很容易混用,而且两者有的时候效果似乎一样,所以我还是用了个简单的测试来简单的比较下. 不多说, ...
- [示例]NSPredicate基础-查询数组中负荷条件的子集
代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...
- ie8解决F12问题
工作中,突然电脑上的ie8按F12掉不出来了,一直显示最小化.于是在网上找了很多方法,按这种方法可以解决问题. 1.cmd+r,输入regedit,调出 注册表编辑器. 2.HKEY_CURRENT_ ...