现在的位置: 首页 > 综合 > 正文

UIImageView异步加载网络图片

2012年07月16日 ⁄ 综合 ⁄ 共 2145字 ⁄ 字号 评论关闭

猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网--Cocos2Dev.com,谢谢!

原文地址: http://www.cocos2dev.com/?p=261


今天要在UIImageView中显示网络的图片,因为UIImageView自身就可以读取网络图片,所以,最简单的做法是:

- (void)viewDidLoad
{
 [super viewDidLoad];

 self.tv_headPic = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)] autorelease];
self.tv_headPic.layer.masksToBounds = YES;
self.tv_headPic.layer.cornerRadius = 5.0f;
 [self.tv_headPic setBackgroundColor:[UIColor grayColor]];
 [self.view addSubview:self.tv_headPic];

 NSURL *imageUrl = [NSURL URLWithString:HEADIMAGE_URL];
 UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageUrl]];
self.tv_headPic.image = image;
}


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


下面就是使用NSOperationQueue实现子线程加载图片:

- (void)viewDidLoad
{
 [super viewDidLoad];

 operationQueue = [[NSOperationQueue alloc] init];

 self.tv_headPic = [[[UIImageView alloc] initWithFrame:CGRectMake(110, 50, 100, 100)] autorelease];
 self.tv_headPic.layer.masksToBounds = YES;
 self.tv_headPic.layer.cornerRadius = 5.0f;
 [self.tv_headPic setBackgroundColor:[UIColor grayColor]];
 [self.view addSubview:self.tv_headPic];

 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.tv_headPic.image = image;
}


不过这也不是最好的设计,因为虽然是异步加载,但是没有缓存图片。重新加载时又要重新从网络读取图片,所以可以考虑保存图片。


1、建立本地缓存目录

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];
 }


2、保存在本地,接着可以用图片名称或者URL或者hash过后的值作为key(本地文件名),写入到本地,我比较喜欢用MD5一下图片url作为文件名保存。

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

下载图片前先判断是否已经缓存了,已经缓存了就不要再去下载了。

抱歉!评论已关闭.