UIImageView异步加载网络图片

时间:2023-03-08 20:03:51

在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,但是发现它不能根据自己的想法去剪裁:有时需要填充整个区域,有时又需要按比例显示。在网上也没找到
相关类库,小弟不才基于EGOImageView自己写了一个私有协议,自己感觉效果不错,之前在论坛一直用别人的代码,今天也共享下。
  有需要的朋友可以看看,可能不是最好,有问题请多多指教。

AutoCutPicSimple_New.zip做了点修改,防止多次seturl导致crash。

也可以直接:https://github.com/hfqf/AutoCutWebPic.git

另一篇:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)] autorelease];
  5. self.imageView.layer.masksToBounds = YES;
  6. self.imageView.layer.cornerRadius = 5.0f;
  7. [self.imageView setBackgroundColor:[UIColor grayColor]];
  8. [self.view addSubview:self.imageView];
  9. NSURL *imageUrl = [NSURL URLWithString:IMAGE_URL];
  10. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
  11. self.imageView.image = image;
  12. }

这么做,最直接的问题就是阻塞UI线程了。

于是考虑利用NSOperationQueue来异步加载图片:

  1. - (void)viewDidLoad
  2. {
  3. [super viewDidLoad];
  4. operationQueue = [[NSOperationQueue alloc] init];
  5. self.imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)] autorelease];
  6. self.imageView.layer.masksToBounds = YES;
  7. self.imageView.layer.cornerRadius = 5.0f;
  8. [self.imageView setBackgroundColor:[UIColor grayColor]];
  9. [self.view addSubview:self.imageView];
  10. NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
  11. [operationQueue addOperation:op];
  12. }
  13. - (void)downloadImage
  14. {
  15. NSURL *imageUrl = [NSURL URLWithString:IMAGE_URL];
  16. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
  17. self.imageView.image = image;
  18. }

这么做的话,就可以避免阻塞UI线程了。当图片异步加载完成后,就会展现出来。

但是,第二次进入该界面,还是要重新下载图片,用户体验不好,且浪费资源(比如耗电)。

于是,考虑缓存已经下载的图片。

模仿操作系统(Cache - Memory - Disk),缓存图片也可以采取两层模型:内存和磁盘。

保存到内存中比较简单,只需要用NSDictionary来维护即可。而保存到磁盘,涉及到本地文件读写,可以参考“文件和数据管理”。

首先需要创建一个缓存目录:

  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  2. diskCachePath = [[[paths objectAtIndex:0] stringByAppendingPathComponent:@"ImageCache"] retain];
  3. if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) {
  4. NSError *error = nil;
  5. [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath
  6. withIntermediateDirectories:YES
  7. attributes:nil
  8. error:&error];
  9. }

接着可以用图片名称或者URL或者hash过后的值作为key(本地文件名),写入到本地:

  1. if (![[NSFileManager defaultManager] fileExistsAtPath:localPath]) {
  2. [[NSFileManager defaultManager] createFileAtPath:localPath contents:localData attributes:nil];
  3. }

这样,在每次下载图片之前,先判断是否已经有缓存了,可以优化体验和性能。

我把完整的源代码写成Category以重用,放在GitHub上:https://github.com/siqin/OnlineImageView 。

Jason Lee @ Hangzhou

Blog: http://blog.csdn.net/jasonblog

第二篇:

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

  1. UIImageView *headview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
  2. NSURL *photourl = [NSURL URLWithString:@"http://www.exampleforphoto.com/pabb/test32.png"];
  3. //url请求实在UI主线程中进行的
  4. UIImage *images = [UIImage imageWithData:[NSData dataWithContentsOfURL:photourl]];//通过网络url获取uiimage
  5. headview.image = images;

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

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

方法2:使用NSOperationQueue异步加载

  1. 下面就是使用NSOperationQueue实现子线程加载图片:
  2. - (void)viewDidLoad
  3. {
  4. [super viewDidLoad];
  5. NSOperationQueue *operationQueue = [[NSOperationQueue alloc] init];
  6. self.imageview  = [[[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)] autorelease];
  7. [self.imageview setBackgroundColor:[UIColor grayColor]];
  8. [self.view addSubview:self.imageview];
  9. NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(downloadImage) object:nil];
  10. [operationQueue addOperation:op];
  11. }
  12. - (void)downloadImage
  13. {
  14. NSURL *imageUrl = [NSURL URLWithString:HEADIMAGE_URL];
  15. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
  16. self.imageview.image = image;
  17. }

不过这这样的设计,虽然是异步加载,但是没有缓存图片。重新加载时又要重新从网络读取图片,重复请求,实在不科学,所以可以考虑第一次请求时保存图片。

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

方法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. //如果目录imageCache不存在,创建目录
  6. if (![[NSFileManager defaultManager] fileExistsAtPath:diskCachePath]) {
  7. NSError *error=nil;
  8. [[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath withIntermediateDirectories:YES attributes:nil error:&error];
  9. }

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

    1. <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];//
    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. NSString *localpath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/imageCache/headimage.png"];
    9. //如果有缓存图片,直接读取cache内的缓存图片
    10. if ([[NSFileManager defaultManager] fileExistsAtPath:localpath]) {
    11. NSData *data = [NSData dataWithContentsOfFile:localpath];
    12. self.headimage = [UIImage imageWithData:data];
    13. [meTableView reloadData];
    14. }
    15. else{
    16. //如果没有缓存图片,请求
    17. NSMutableDictionary *headdict =[NSMutableDictionary dictionaryWithObjectsAndKeys:tokenString,@"token",uidString,@"userid",nil];
    18. [self handlerPostFormDataWithParams:headdict withURL:APPROVAL_URL_GETPHOTO];
    19. }</pre><br>
    20. 方法3是最科学的,只需要一次读取,以后就可以直接用了。<p></p>
    21. <p><span style="font-size:18px"><br>
    22. </span></p>
    23. <p><span style="font-size:18px">目前总结到这里,以后有更深入研究时再补充</span></p>
    24. <p><span style="font-size:18px">20130314<br>
    25. <br>
    26. <br>
    27. </span><br>
    28. </p>