Skip to content

Instantly share code, notes, and snippets.

@jbrennan
Created June 26, 2011 02:03
Show Gist options
  • Save jbrennan/1047140 to your computer and use it in GitHub Desktop.
Save jbrennan/1047140 to your computer and use it in GitHub Desktop.
Yo dawg, I heard you like Blocks
// Loads the full-sized image ... might be big.
- (void)originalImageWithCompletionHandler:(DownloadedImageCompletionHandler)completionHandler {
// First check to see if this image is cached in memory, if execute the handler passing in the image.
if (nil != _originalImage) {
if (completionHandler) {
completionHandler(self, _photo, _photo.urlOriginal, _originalImage);
}
return;
}
// then check to see if this image already exists on disk, in which case, it should be loaded and the completion handler should be invoked.
NSString *photoPath = [self storagePathForImageSize:ICDownloadedImageSizeOriginal];
NSData *photoData = [NSData dataWithContentsOfFile:photoPath];
// if photoData *isn't* nil, that means the file existed on disk (ie. had alread been loaded), and we can now execute the completion handler
if (nil != photoData) {
self.originalImage = [UIImage imageWithData:photoData]; // So we have this image cached in disk, we retain it
if (completionHandler) {
completionHandler(self, _photo, _photo.urlOriginal, _originalImage);
}
return;
}
// At this stage, the image wasn't in memory, and the image wasn't cached to disk, either, so it needs to be downloaded
__block __typeof__(self) unretainedSelf = self;
_connection = [[HTTPConnection alloc] initWithURLString:_photo.urlOriginal userInfo:nil completionHandler:^(HTTPConnection *connection, NSError *error, NSString *urlString, NSDictionary *userInfo, NSData *data) {
if (nil != error) {
NSLog(@"Error loading full sized image: %@", [error userInfo]);
// Execute the DownloadedImageCompletionHandler, passing nil in for the image.
if (completionHandler) {
completionHandler(unretainedSelf, _photo, _photo.urlOriginal, nil);
}
return;
}
// No error with the connection, we have the data back, let's save the data to disk for future reference,
// Then create an image with that, and execute the above completion handler with that data.
// We can access the same photoPath from above (sneaky blocks are sneaky....)
NSError *saveError = nil;
if (![data writeToFile:photoPath options:NSDataWritingAtomic error:&saveError]) {
NSLog(@"There was an error writing the downloaded image file to disk. The error: %@", [error userInfo]);
// Execute the completion handler passing nil for the image, to indicate failure
if (completionHandler) {
completionHandler(unretainedSelf, _photo, _photo.urlOriginal, nil);
}
return;
}
// If the data was successfully written to disk, this means all is good.
// We can now create a UIImage and execute the completion handler with that.
unretainedSelf.originalImage = [UIImage imageWithData:data];
if (completionHandler) {
completionHandler(unretainedSelf, _photo, _photo.urlOriginal, unretainedSelf.originalImage);
}
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment