Skip to content

Instantly share code, notes, and snippets.

@jankeesvw
Created September 5, 2010 19:21
Show Gist options
  • Save jankeesvw/566252 to your computer and use it in GitHub Desktop.
Save jankeesvw/566252 to your computer and use it in GitHub Desktop.
//Add this as an ivar
NSOperationQueue *imageLoadingQueue;
//Add this to your dealloc method
[imageLoadingQueue release];
//If you're creating the nib programatically use this
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if (self = [super initWithNibName:nibName bundle:nibBundle]) {
imageLoadingQueue = [[NSOperationQueue alloc] init];
}
return self;
}
//Otherwise use this version (or put both in just in case)
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
imageLoadingQueue = [[NSOperationQueue alloc] init];
}
return self;
}
- (void)gamekingsLiteDataWasLoaded {
[self loadImageFromURL:[NSURL URLWithString:[dataLoader appPoster]]];
}
- (void)loadImageFromURL:(NSURL *)aImageURL {
//Perform this in the background
[imageLoadingQueue addOperationWithBlock:^{
NSError *error = nil;
NSData *imageData = [NSData dataWithContentsOfURL:aImageURL options:NSDataReadingUncached error:&error];
//Perform the following on the main thread after we've downloaded the data (otherwise bad things will happen)
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if (imageData) {
UIImage *image = [UIImage imageWithData:imageData];
[imageView setImage:image];
continueButton.alpha = 1;
} else {
NSLog(@"need to handle error:%@", error);
}
}];
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment