Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2608464
  • 博文数量: 877
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 5921
  • 用 户 组: 普通用户
  • 注册时间: 2013-12-05 12:25
个人简介

技术的乐趣在于分享,欢迎多多交流,多多沟通。

文章分类

全部博文(877)

文章存档

2021年(2)

2016年(20)

2015年(471)

2014年(358)

2013年(26)

分类: iOS平台

2015-09-02 11:16:36

UIImageView异步加载网络图片
http://blog.csdn.net/mad1989/article/details/8674962

方法1:在UI线程中同步加载网络图片


  1. UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];  
  2.   
  3. NSURL *photourl = [NSURL URLWithString:@" style="margin:0px;padding:0px;border:none;background-color:inherit;">];  
  4. //url请求实在UI主线程中进行的  
  5. UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage  
  6. headview.image = images;  



这是最简单的,但是由于在主线程中加载,会阻塞UI主线程。所以可以试试NSOperationQueue,一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。


------------------------------------------------------------------------------------------------


方法2:使用NSOperationQueue异步加载

  1. 下面就是使用NSOperationQueue实现子线程加载图片:  
  2. - (void)viewDidLoad  
  3. {  
  4.     [super viewDidLoad];  
  5.       
  6.     NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];  
  7.       
  8.     self.imageview  = [[[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)] autorelease];  
  9.     [self.imageview setBackgroundColor:[UIColor grayColor]];  
  10.     [self.view addSubview:self.imageview];  
  11.       
  12.     NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];  
  13.     [operationQueue addOperation:op];  
  14. }  
  15.   
  16. - (void)downloadImage  
  17. {  
  18.     NSURL *imageUrl = [NSURL URLWithString:HEADIMAGE_URL];  
  19.     UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];  
  20.     self.imageview.image = image;  
  21. }  
不过这这样的设计,虽然是异步加载,但是没有缓存图片。重新加载时又要重新从网络读取图片,重复请求,实在不科学,所以可以考虑第一次请求时保存图片。



------------------------------------------------------------------------------------------------


方法3:异步加载,保存到cache里,下次再请求时读取cache里的缓存图片


3-1,首先在http请求时,建立一个缓存目录

  1. NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  2. NSLog(@"the paths:%@",paths);  
  3. NSString * diskCachePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"imageCache"];  
  4. NSLog(@"diskCachePath:%@",diskCachePath);  
  5.   
  6. //如果目录imageCache不存在,创建目录  
  7. if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) {  
  8.     NSError *error=nil;  
  9.     [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath withIntermediateDirectories:YES attributes:nil error:&error];  
  10.       
  11. }  

3-2创建好目录后,第一次时,url请求网络图片,然后把得到的data保存到本地cache里。


  1.        <span style="white-space:pre">   </span>   NSURL *headurl = [NSURL URLWithString:<pre name="code" class="cpp">@" style="margin:0px;padding:0px;border:none;background-color:inherit;"><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];//  
  2.  NSLog(@"localpaht:%@",localPath); if (imagedata) { if (![[NSFileManager defaultManager] fileExistsAtPath:localPath]) { [[NSFileManager defaultManager] createFileAtPath:localPath contents:imagedata attributes:nil]; //[imagedata writeToFile:localPath atomically:YES];//也可以data  
  3.  write到file里 } UIImage *headimg = [[UIImage alloc] initWithData:imagedata]; self.headimage = headimg; [headimg release]; [meTableView reloadData]; }<p></p>  
  4. <pre></pre>  
  5. 3-3然后在下一次打开,初始化设置图片的地方,判断cache里有没有缓存图片,有的话直接加载。  
  6. <p></p>  
  7. <p><span style="font-size:18px"></span></p><pre name="code" class="cpp">    NSArray *paths =NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);  
  8.       
  9.     NSString *localpath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/imageCache/headimage.png"];  
  10.       
  11.     //如果有缓存图片,直接读取cache内的缓存图片  
  12.     if ([[NSFileManager defaultManager] fileExistsAtPath:localpath]) {  
  13.         NSData *data = [NSData dataWithContentsOfFile:localpath];  
  14.   
  15.           
  16.         self.headimage = [UIImage imageWithData:data];  
  17.         [meTableView reloadData];  
  18.           
  19.     }  
  20.     else{  
  21.         //如果没有缓存图片,请求  
  22.         NSMutableDictionary *headdict =[NSMutableDictionary dictionaryWithObjectsAndKeys:tokenString,@"token",uidString,@"userid",nil];  
  23.         [self handlerPostFormDataWithParams:headdict withURL:APPROVAL_URL_GETPHOTO];  
  24.     }</pre><br>  
  25. 方法3是最科学的,只需要一次读取,以后就可以直接用了。<p></p>  
  26. <p><span style="font-size:18px"><br>  
  27. </span></p>  
  28. <p><span style="font-size:18px">目前总结到这里,以后有更深入研究时再补充</span></p>  
  29. <p><span style="font-size:18px">20130314<br>  
  30. <br>  
  31. <br>  
  32. </span><br>  
  33. </p>  
  34.       
  35.         <div style="padding-top:20px">           
  36.             <p style="font-size:12px;">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>  
  37.         </div>  
阅读(589) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~