加载图像到UIImage的异步 [英] Loading an image into UIImage asynchronously
问题描述
我正在开发的iOS 5.0 SDK和X code 4.2的iOS 4的应用程序。
I'm developing an iOS 4 application with iOS 5.0 SDK and XCode 4.2.
我要表现出一定的发表博客变成一个UITableView。当我retreived所有Web服务数据,我用这个方法来创建一个的UITableViewCell:
I have to show some post blogs into a UITableView. When I have retreived all web service data, I use this method to create an UITableViewCell:
- (BlogTableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString* cellIdentifier = @"BlogCell";
BlogTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray* topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"BlogTableViewCell" owner:nil options:nil];
for(id currentObject in topLevelObjects)
{
if ([currentObject isKindOfClass:[BlogTableViewCell class]])
{
cell = (BlogTableViewCell *)currentObject;
break;
}
}
}
BlogEntry* entry = [blogEntries objectAtIndex:indexPath.row];
cell.title.text = entry.title;
cell.text.text = entry.text;
cell.photo.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:entry.photo]]];
return cell;
}
但这一行:
cell.photo.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:entry.photo]]];
它是如此之慢(entry.photo有一个HTTP URL)。
it is so slow (entry.photo has a http url).
有什么方法,可以异步加载图像?我认为这是困难的,因为的tableView:的cellForRowAtIndexPath被称为非常频繁
Is there any way to load that image asynchronously? I think it is difficult because tableView:cellForRowAtIndexPath is called very often.
推荐答案
看看SDWebImage:
Take a look at SDWebImage:
https://github.com/rs/SDWebImage
这是一个梦幻般的一套处理一切为你的类。
It's a fantastic set of classes that handle everything for you.
添
这篇关于加载图像到UIImage的异步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!