-
-
Save syxc/5546191 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
// | |
// UIImage+Network.h | |
// Fireside | |
// | |
// Created by Soroush Khanlou on 8/25/12. | |
// | |
// | |
#import <UIKit/UIKit.h> | |
@interface UIImageView(Network) | |
@property (nonatomic, copy) NSURL *imageURL; | |
- (void) loadImageFromURL:(NSURL*)url placeholderImage:(UIImage*)placeholder cachingKey:(NSString*)key; | |
@end |
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+Network.m | |
// | |
// Created by Soroush Khanlou on 8/25/12. | |
// | |
// | |
#import "UIImageView+Network.h" | |
#import "FTWCache.h" | |
#import <objc/runtime.h> | |
static char URL_KEY; | |
@implementation UIImageView(Network) | |
@dynamic imageURL; | |
- (void) loadImageFromURL:(NSURL*)url placeholderImage:(UIImage*)placeholder cachingKey:(NSString*)key { | |
self.imageURL = url; | |
self.image = placeholder; | |
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul); | |
dispatch_async(queue, ^{ | |
NSData *data = [NSData dataWithContentsOfURL:url]; | |
UIImage *imageFromData = [UIImage imageWithData:data]; | |
[FTWCache setObject:data forKey:key]; | |
if (imageFromData) { | |
if ([self.imageURL.absoluteString isEqualToString:url.absoluteString]) { | |
dispatch_sync(dispatch_get_main_queue(), ^{ | |
self.image = imageFromData; | |
}); | |
} else { | |
// NSLog(@"urls are not the same, bailing out!"); | |
} | |
} | |
self.imageURL = nil; | |
}); | |
} | |
- (void) setImageURL:(NSURL *)newImageURL { | |
objc_setAssociatedObject(self, &URL_KEY, newImageURL, OBJC_ASSOCIATION_COPY); | |
} | |
- (NSURL*) imageURL { | |
return objc_getAssociatedObject(self, &URL_KEY); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment