VansFannel VansFannel - 6 months ago 48
iOS Question

Loading an image into UIImage asynchronously

I'm developing an iOS 4 application with iOS 5.0 SDK and XCode 4.2.

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;

BlogEntry* entry = [blogEntries objectAtIndex:indexPath.row];

cell.title.text = entry.title;
cell.text.text = entry.text; = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL]]];

return cell;

But this line: = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL]]];

it is so slow ( has a http url).

Is there any way to load that image asynchronously? I think it is difficult because tableView:cellForRowAtIndexPath is called very often.


Take a look at SDWebImage:

It's a fantastic set of classes that handle everything for you.