Created
February 7, 2011 19:38
-
-
Save ahmetardal/815029 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
/*! | |
@method | |
@abstract adds images to the queue and starts the operation queue to download them | |
*/ | |
- (void) addImagesToQueue:(NSArray *)images | |
{ | |
NSLog(@"AsyncImageLoadingViewController::addImagesToQueue called"); | |
[self.imageQueue addObjectsFromArray:images]; | |
// suspend the operation queue | |
[self.imageLoaderOpQueue setSuspended:YES]; | |
// add tasks to the operation queue | |
for (NSString *imageUrl in self.imageQueue) { | |
NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self | |
selector:@selector(loadImage:) | |
object:imageUrl]; | |
[self.imageLoaderOpQueue addOperation:op]; | |
[op release]; | |
} | |
// clear items in the queue and resume the operation queue to start downloading images | |
[self.imageQueue removeAllObjects]; | |
[self.imageLoaderOpQueue setSuspended:NO]; | |
} |
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
/*! | |
@method | |
@abstract downloads images, this is the method that dispatches tasks in the operation queue | |
*/ | |
- (void) loadImage:(id)arg | |
{ | |
NSLog(@"AsyncImageLoadingViewController::loadImage called"); | |
if ((arg == nil) || ([arg isKindOfClass:[NSString class]] == NO)) { | |
return; | |
} | |
// create a local autorelease pool since this code runs not on main thread | |
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; | |
// fetch the image | |
NSLog(@"AsyncImageLoadingViewController::loadImage - will download image: %@", arg); | |
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:arg]]; | |
UIImage *image = [UIImage imageWithData:data]; | |
// update tableview with the downloaded image on main thread | |
[self performSelectorOnMainThread:@selector(updateTableView:) withObject:[image retain] waitUntilDone:NO]; | |
[pool release]; | |
} |
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
/*! | |
@method | |
@abstract updates tableview for the newly downloaded image and scrolls the tableview to bottom | |
*/ | |
- (void) updateTableView:(id)arg | |
{ | |
NSLog(@"AsyncImageLoadingViewController::updateTableView called"); | |
if ((arg == nil) || ([arg isKindOfClass:[UIImage class]] == NO)) { | |
return; | |
} | |
// store the newly downloaded image | |
[self.loadedImages addObject:arg]; | |
[arg release]; | |
// refresh tableview | |
[self.imagesTableView reloadData]; | |
// scroll to the last cell of the tableview | |
NSIndexPath *lastRow = [NSIndexPath indexPathForRow:([self.loadedImages count] - 1) inSection:0]; | |
[self.imagesTableView scrollToRowAtIndexPath:lastRow | |
atScrollPosition:UITableViewScrollPositionBottom | |
animated:YES]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment