-
-
Save fdln/277174f9a963dd5dca17 to your computer and use it in GitHub Desktop.
Download image and update using NSURLSession
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
@interface NBNet() | |
@property(nonatomic, strong) NSURLSession *imageSession; | |
@property(nonatomic, strong) NSOperationQueue *netOperationQueue; | |
@end | |
@implementation NBNet | |
+ (id)sharedNBNet { | |
static NBNet *sharedNBNet = nil; | |
static dispatch_once_t onceToken; | |
dispatch_once(&onceToken, ^{ | |
sharedNBNet = [[self alloc] init]; | |
}); | |
return sharedNBNet; | |
} | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) { | |
_netOperationQueue = [[NSOperationQueue alloc] init]; | |
_netOperationQueue.maxConcurrentOperationCount = 3; | |
_netOperationQueue.name = @"Net Operations"; | |
NSURLSessionConfiguration *sessionImageConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
sessionImageConfiguration.timeoutIntervalForResource = 6; | |
sessionImageConfiguration.HTTPMaximumConnectionsPerHost = 2; | |
sessionImageConfiguration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy; | |
_imageSession = [NSURLSession sessionWithConfiguration:sessionImageConfiguration delegate:nil delegateQueue:_netOperationQueue]; | |
} | |
return self; | |
} | |
- (NSURLSessionTask *)imageWithURL:(NSURL *)url | |
success:(void (^)(UIImage *image))success | |
failure:(void (^)(NSError *error))failure { | |
NSURLSessionTask *task = [_imageSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { | |
if (error) | |
return failure(error); | |
if (response) | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
//useless optimization as it seems to be decoded while UIImageView is displayed | |
UIImage *image = [UIImage imageWithData:data]; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
if (image) | |
success(image); | |
}); | |
}); | |
}]; | |
[task resume]; | |
return task; | |
} | |
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
#import <UIKit/UIKit.h> | |
static char const * const taskKey = "TaskKey"; | |
@interface UIImageView (NBNet) | |
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; | |
- (void)cancelDownload; | |
@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
#import "UIImageView+NBNet.h" | |
#import "NBNet.h" | |
#import <objc/runtime.h> | |
@interface UIImageView (NBNetProperty) | |
@property(nonatomic, strong) NSURLSessionTask *task; | |
@end | |
@implementation UIImageView (NBNet) | |
- (NSURLSessionTask *)task { | |
return objc_getAssociatedObject(self, | |
&taskKey); | |
} | |
- (void)setTask:(NSURLSessionTask *)newTask { | |
objc_setAssociatedObject(self, | |
&taskKey, | |
newTask, | |
OBJC_ASSOCIATION_COPY); | |
} | |
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder { | |
self.image = placeholder; | |
NBNet *net = [NBNet sharedNBNet]; | |
NSURLSessionTask *dTask = [net imageWithURL:url success:^(UIImage *image) { | |
self.image = image; | |
} failure:^(NSError *error) { | |
DLOGV(4, net, error); | |
}]; | |
self.task = dTask; | |
} | |
- (void)cancelDownload { | |
[self.task cancel]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment