If you like my tutorials, you will love my book . It is chockful of advanced programming techniques and the only comprehensive barcode reference for serious iOS developers.
Our DNA is written in Objective-C

A Quick Lazy-Loading UIImageView

In case you ever need to enhance UIImageView to load it’s image lazily… like if it’s coming from a web URL.

I used this on my NSAttributedStrings+HTML open source project to improve performance and also be able to have images that are not available locally. But you don’t want to do a synchronous call over the web, that would block your UI.

I admit, it’s a quickie, but hey, sometimes those are also fun!


@interface DTLazyImageView : UIImageView 
	NSURL *_url;
	BOOL _loading;
@property (nonatomic, retain) NSURL *url;


#import "DTLazyImageView.h"
@implementation DTLazyImageView
- (void)dealloc
	self.image = nil;
	[_url release];
	[super dealloc];
- (void)loadImageAtURL:(NSURL *)url
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSData *data = [[NSData alloc] initWithContentsOfURL:url];
	if (data)
		UIImage *image = [[UIImage alloc] initWithData:data];
		[self performSelectorOnMainThread:@selector(setImage:) withObject:image 
		[image release];
	[data release];
	_loading = NO;
	[pool release];
- (void)didMoveToSuperview
	if (!self.image && _url && !_loading)
		_loading = YES;
		[self performSelectorInBackground:@selector(loadImageAtURL:) withObject:_url];
#pragma mark Properties
@synthesize url = _url;

You can see, it’s very simple. When the view is added to a super view by means of addSubview then it starts loading the image on a background thread. Setting the image has to occur on the main thread because UIKit is not threadsafe, or at least has not been until recently.

Categories: Recipes

%d bloggers like this: