UIImageView异步加载网络图片

猴子原创,欢迎转载。转载请注明:转载自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];
 }

下载图片前先判断是否已经缓存了,已经缓存了就不要再去下载了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值