Created
September 5, 2010 19:21
-
-
Save jankeesvw/566252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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