Last active
June 20, 2016 22:34
-
-
Save greenhouse/a7213e796d887ae90444ce74f9bd0ee9 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
// | |
// UIImageView+Networking.m | |
// greenhouse | |
// | |
// Created by greenhouse on 7/9/15. | |
// | |
#import "UIImageView+Networking.h" | |
#import "xlogger.h" | |
#import "CPFilePathDefines.h" | |
@implementation UIImageView (Networking) | |
- (void)setImageAsyncWithUrlString:(NSString*)urlString | |
placeholder:(UIImage*)placeholder | |
alternate:(UIImage*)alternate | |
cacheLocation:(id)cacheLocation | |
completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock | |
{ | |
if (!urlString || !placeholder || !alternate) | |
{ | |
XLM_warning(@"invalid parameters; attempting to set alternate, otherwise UIImageView.image will not be set!"); | |
self.image = alternate ? alternate : [UIImage imageNamed:f_imgNoPhoto]; | |
completionBlock(NO, alternate); | |
return; | |
} | |
// first set placeholder image to this ImageView | |
self.image = placeholder; | |
// make async call in background to get the image from the url | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
BOOL success = YES; | |
// retreive image | |
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]]]; | |
if (!img) | |
{ | |
// if can't retreive, fall back to alternate image | |
img = alternate; | |
success = NO; | |
} | |
// example with cacheLocation | |
//[cacheLocation setCellImagesWithArray:@[img]]; | |
// perform completion block back on the main thread (update ui stuff) | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
// set returned/alternate image to this ImageView | |
self.image = img; | |
// execute completion block | |
completionBlock(success, img); | |
}); | |
}); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment