This post is a simplified version of the previous submitted post: A simple COCOA Asynchronous image loader class to use in your iPhone app
This method uses blocks so will run on iOS4 onwards:
void UIImageFromURL( NSURL * URL, void (^imageBlock)(UIImage * image), void (^errorBlock)(void) )
{
dispatch_async( dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^(void)
{
NSData * data = [[[NSData alloc] initWithContentsOfURL:URL] autorelease];
UIImage * image = [[[UIImage alloc] initWithData:data] autorelease];
dispatch_async( dispatch_get_main_queue(), ^(void){
if( image != nil )
{
imageBlock( image );
} else {
errorBlock();
}
});
});
}
// usage
UIImageFromURL( [NSURL URLWithString:@"image url here"], ^( UIImage * image )
{
NSLog(@"%@",image);
}, ^(void){
NSLog(@"%@",@"error!");
});





While this is a very simple way of doing this, it doesn’t give the end developer the ability to figure out which kind of error occurred, only that it occurred. This is usually a bad idea. For example, because UIKit may not be able to create a UIImage instance with the data that was retrieved from the network, it still may have been downloaded and in memory and may possibly be usable in some other context or with some more tinkering. My point is that you shouldn’t use such a routine in a shipping app if you know what’s good for you. Just my two cents.
If you use this for reuseable table view cells, watch out because you can potentially have several blocks being executed on a reused cell, which would cause your UIImageView’s to flickr.
In addition to what Steven said… use in a table view is a problem because there is no way to cancel the request. Also there is no caching with this method. A custom class that uses NSURLConnection would be more flexible. You could still pass in blocks to handle the response/error cases. This would give you better error handling and the ability to cancel the request. IMHO