Skip to content

Instantly share code, notes, and snippets.

@CavalcanteLeo
Created December 21, 2017 03:04
Show Gist options
  • Save CavalcanteLeo/5e58f510122e2a6ef2d837c96397c1cf to your computer and use it in GitHub Desktop.
Save CavalcanteLeo/5e58f510122e2a6ef2d837c96397c1cf to your computer and use it in GitHub Desktop.
DownloadProgressPinRemoteImageManager
#import "PINRemoteImageManager.h"
@interface RemoteImageManager : PINRemoteImageManager
+ (instancetype)sharedImageManager;
typedef void (^PINRemoteImageManagerImagePercentageProgressBlock)(float percent);
- (NSUUID *)downloadImageWithURL:(NSURL *)url
options:(PINRemoteImageManagerDownloadOptions)options
percentProgress:(PINRemoteImageManagerImagePercentageProgressBlock)percentProgress
progress:(PINRemoteImageManagerImageCompletion)progress
completion:(PINRemoteImageManagerImageCompletion)completion;
@end
#import "RemoteImageManager.h"
#import "PINCache.h"
#import "PINRemoteImageDownloadTask.h"
@interface PINRemoteImageManager()
@property (nonatomic, strong) NSMutableDictionary *tasks;
- (void)didReceiveData:(NSData *)data forTask:(NSURLSessionDataTask *)dataTask;
- (NSUUID *)downloadImageWithURL:(NSURL *)URL
options:(PINRemoteImageManagerDownloadOptions)options
priority:(PINRemoteImageManagerPriority)priority
processorKey:(NSString *)processorKey
processor:(PINRemoteImageManagerImageProcessor)processor
progress:(PINRemoteImageManagerImageCompletion)progress
completion:(PINRemoteImageManagerImageCompletion)completion
inputUUID:(NSUUID *)UUID;
@end
@interface RemoteImageManager() {
}
@property (nonatomic, strong) NSMutableDictionary *percentProgressBlocks;
@end
@implementation RemoteImageManager
+ (instancetype)sharedImageManager
{
static RemoteImageManager *sharedImageManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedImageManager = [[[self class] alloc] init];
});
return sharedImageManager;
}
- (instancetype)init {
if (self = [super init]) {
_percentProgressBlocks = [NSMutableDictionary dictionary];
}
return self;
}
- (void)didReceiveData:(NSData *)data forTask:(NSURLSessionDataTask *)dataTask {
[super didReceiveData:data forTask:dataTask];
if (dataTask.countOfBytesExpectedToReceive <= 0) {
return ;
}
NSString *key = [self cacheKeyForURL:[[dataTask originalRequest] URL] processorKey:nil];
PINRemoteImageManagerImagePercentageProgressBlock percentProgressBlock = [_percentProgressBlocks objectForKey:key];
if (!percentProgressBlock) {
return ;
}
float percent = dataTask.countOfBytesReceived/(float)dataTask.countOfBytesExpectedToReceive;
dispatch_async(dispatch_get_main_queue(), ^() {
if (!percentProgressBlock) {
return ;
}
percentProgressBlock(percent);
});
}
- (NSUUID *)downloadImageWithURL:(NSURL *)url
options:(PINRemoteImageManagerDownloadOptions)options
percentProgress:(PINRemoteImageManagerImagePercentageProgressBlock)percentProgress
progress:(PINRemoteImageManagerImageCompletion)progress
completion:(PINRemoteImageManagerImageCompletion)completion {
NSString *key = [self cacheKeyForURL:url processorKey:nil];
[_percentProgressBlocks setObject:percentProgress forKey:key];
__weak RemoteImageManager *weakSelf = self;
NSUUID *uuid = [self downloadImageWithURL:url options:options progress:progress completion:^(PINRemoteImageManagerResult *result) {
completion(result);
[weakSelf.percentProgressBlocks removeObjectForKey:key];
}];
return uuid;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment