Skip to content

Instantly share code, notes, and snippets.

@akhenakh
Last active March 5, 2017 01:45
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save akhenakh/7027747 to your computer and use it in GitHub Desktop.
Save akhenakh/7027747 to your computer and use it in GitHub Desktop.
Download image and update using NSURLSession
@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;
}
#import <UIKit/UIKit.h>
static char const * const taskKey = "TaskKey";
@interface UIImageView (NBNet)
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
- (void)cancelDownload;
@end
#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
@akhenakh
Copy link
Author

TODO:// set the placeholder back in cancelDownload ?

@evgenyneu
Copy link

Very useful gist, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment