Skip to content

Commit

Permalink
Merge branch 'maicki-feature/add-download-progress'
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettmoon committed Feb 11, 2016
2 parents 2ec7ea2 + b57161d commit 2d7b0f9
Show file tree
Hide file tree
Showing 10 changed files with 185 additions and 45 deletions.
2 changes: 1 addition & 1 deletion Example-Mac/PINRemoteImage/ProgressiveViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ - (void)viewDidAppear
NSMutableArray *progress = [[NSMutableArray alloc] init];
[[PINRemoteImageManager sharedImageManager]
downloadImageWithURL:progressiveURL
options:PINRemoteImageManagerDownloadOptionsNone progress:^(PINRemoteImageManagerResult *result) {
options:PINRemoteImageManagerDownloadOptionsNone progressImage:^(PINRemoteImageManagerResult *result) {
[progress addObject:result.image];
} completion:^(PINRemoteImageManagerResult *result) {
[progress addObject:result.image];
Expand Down
12 changes: 6 additions & 6 deletions Example/PINRemoteImage Tests/PINRemoteImage_Tests.m
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,7 @@ - (void)testQOS
__block UIImage *image;
[self.imageManager downloadImageWithURLs:@[[self JPEGURL_Small], [self JPEGURL_Medium], [self JPEGURL_Large]]
options:PINRemoteImageManagerDownloadOptionsNone
progress:nil
progressImage:nil
completion:^(PINRemoteImageManagerResult *result)
{
image = result.image;
Expand All @@ -699,7 +699,7 @@ - (void)testQOS
[self.imageManager setCurrentBytesPerSecond:5];
[self.imageManager downloadImageWithURLs:@[[self JPEGURL_Small], [self JPEGURL_Medium], [self JPEGURL_Large]]
options:PINRemoteImageManagerDownloadOptionsNone
progress:nil
progressImage:nil
completion:^(PINRemoteImageManagerResult *result)
{
image = result.image;
Expand All @@ -711,7 +711,7 @@ - (void)testQOS
[self.imageManager.cache removeAllObjects];
[self.imageManager downloadImageWithURLs:@[[self JPEGURL_Small], [self JPEGURL_Medium], [self JPEGURL_Large]]
options:PINRemoteImageManagerDownloadOptionsNone
progress:nil
progressImage:nil
completion:^(PINRemoteImageManagerResult *result)
{
image = result.image;
Expand All @@ -725,7 +725,7 @@ - (void)testQOS
[self.imageManager setCurrentBytesPerSecond:100];
[self.imageManager downloadImageWithURLs:@[[self JPEGURL_Small], [self JPEGURL_Medium], [self JPEGURL_Large]]
options:PINRemoteImageManagerDownloadOptionsNone
progress:nil
progressImage:nil
completion:^(PINRemoteImageManagerResult *result)
{
image = result.image;
Expand All @@ -742,7 +742,7 @@ - (void)testQOS
[self.imageManager setCurrentBytesPerSecond:7];
[self.imageManager downloadImageWithURLs:@[[self JPEGURL_Small], [self JPEGURL_Medium], [self JPEGURL_Large]]
options:PINRemoteImageManagerDownloadOptionsNone
progress:nil
progressImage:nil
completion:^(PINRemoteImageManagerResult *result)
{
image = result.image;
Expand Down Expand Up @@ -770,7 +770,7 @@ - (void)testQOS
[self.imageManager setCurrentBytesPerSecond:7];
[self.imageManager downloadImageWithURLs:@[[self JPEGURL_Small], [self JPEGURL_Large]]
options:PINRemoteImageManagerDownloadOptionsNone
progress:nil
progressImage:nil
completion:^(PINRemoteImageManagerResult *result)
{
image = result.image;
Expand Down
2 changes: 1 addition & 1 deletion Example/PINRemoteImage/ProgressiveViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ - (void)viewDidAppear:(BOOL)animated
NSMutableArray *progress = [[NSMutableArray alloc] init];
[[PINRemoteImageManager sharedImageManager]
downloadImageWithURL:progressiveURL
options:PINRemoteImageManagerDownloadOptionsNone progress:^(PINRemoteImageManagerResult *result) {
options:PINRemoteImageManagerDownloadOptionsNone progressImage:^(PINRemoteImageManagerResult *result) {
[progress addObject:result.image];
} completion:^(PINRemoteImageManagerResult *result) {
[progress addObject:result.image];
Expand Down
3 changes: 2 additions & 1 deletion Pod/Classes/PINRemoteImageCallbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
@interface PINRemoteImageCallbacks : NSObject

@property (nonatomic, strong, nullable) PINRemoteImageManagerImageCompletion completionBlock;
@property (nonatomic, strong, nullable) PINRemoteImageManagerImageCompletion progressBlock;
@property (nonatomic, strong, nullable) PINRemoteImageManagerImageCompletion progressImageBlock;
@property (nonatomic, strong, nullable) PINRemoteImageManagerProgressDownload progressDownloadBlock;
@property (nonatomic, assign) CFTimeInterval requestTime;

@end
4 changes: 2 additions & 2 deletions Pod/Classes/PINRemoteImageCategoryManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ + (void)setImageOnView:(id <PINRemoteImageCategory>)view
if (urls.count > 1) {
downloadImageOperationUUID = [[PINRemoteImageManager sharedImageManager] downloadImageWithURLs:urls
options:options
progress:internalProgress
progressImage:internalProgress
completion:internalCompletion];
} else if (processorKey.length > 0 && processor) {
downloadImageOperationUUID = [[PINRemoteImageManager sharedImageManager] downloadImageWithURL:urls[0]
Expand All @@ -270,7 +270,7 @@ + (void)setImageOnView:(id <PINRemoteImageCategory>)view
} else {
downloadImageOperationUUID = [[PINRemoteImageManager sharedImageManager] downloadImageWithURL:urls[0]
options:options
progress:internalProgress
progressImage:internalProgress
completion:internalCompletion];
}

Expand Down
15 changes: 8 additions & 7 deletions Pod/Classes/PINRemoteImageDownloadTask.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ - (BOOL)hasProgressBlocks
{
__block BOOL hasProgressBlocks = NO;
[self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
if (callback.progressBlock) {
if (callback.progressImageBlock) {
hasProgressBlocks = YES;
*stop = YES;
}
Expand All @@ -42,15 +42,16 @@ - (BOOL)hasProgressBlocks
- (void)callProgressWithQueue:(dispatch_queue_t)queue withImage:(PINImage *)image
{
[self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
if (callback.progressBlock != nil) {
if (callback.progressImageBlock != nil) {
PINLog(@"calling progress for UUID: %@ key: %@", UUID, self.key);
dispatch_async(queue, ^
{
callback.progressBlock([PINRemoteImageManagerResult imageResultWithImage:image
animatedImage:nil
requestLength:CACurrentMediaTime() - callback.requestTime
error:nil
resultType:PINRemoteImageResultTypeProgress UUID:UUID]);
callback.progressImageBlock([PINRemoteImageManagerResult imageResultWithImage:image
animatedImage:nil
requestLength:CACurrentMediaTime() - callback.requestTime
error:nil
resultType:PINRemoteImageResultTypeProgress
UUID:UUID]);
});
}
}];
Expand Down
77 changes: 68 additions & 9 deletions Pod/Classes/PINRemoteImageManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ typedef void(^PINRemoteImageManagerAuthenticationChallengeCompletionHandler)(NSU
*/
typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * __nullable task, NSURLAuthenticationChallenge * __nonnull challenge, PINRemoteImageManagerAuthenticationChallengeCompletionHandler __nullable aHandler);

/**
Handler called for many PINRemoteImage tasks providing the progress of the download.
@param completedBytes Amount of bytes that have been downloaded so far.
@param totalBytes Total amount of bytes in the image being downloaded.
*/
typedef void(^PINRemoteImageManagerProgressDownload)(NSInteger completedBytes, NSInteger totalBytes);

@interface PINRemoteImageManager : NSObject

@property (nonatomic, readonly, nonnull) PINCache * cache;
Expand Down Expand Up @@ -155,7 +163,7 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _

/**
Set the minimum BPS to download the highest quality image in a set.
@see downloadImageWithURLs:options:progress:completion:
@see downloadImageWithURLs:options:progressImage:completion:
@param highQualityBPSThreshold bytes per second minimum. Defaults to 500000.
@param completion Completion to be called once highQualityBPSThreshold has been set.
Expand All @@ -164,7 +172,7 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _

/**
Set the maximum BPS to download the lowest quality image in a set.
@see downloadImageWithURLs:options:progress:completion:
@see downloadImageWithURLs:options:progressImage:completion:
@param lowQualityBPSThreshold bytes per second maximum. Defaults to 50000.
@param completion Completion to be called once lowQualityBPSThreshold has been set.
Expand All @@ -174,7 +182,7 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _

/**
Set whether high quality images should be downloaded when a low quality image is cached if network connectivity has improved.
@see downloadImageWithURLs:options:progress:completion:
@see downloadImageWithURLs:options:progressImage:completion:
@param shouldUpgradeLowQualityImages if YES, low quality images will be 'upgraded'.
@param completion Completion to be called once shouldUpgradeLowQualityImages has been set.
Expand Down Expand Up @@ -292,14 +300,63 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _
@param url NSURL where the image to download resides.
@param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
@param progress PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
@param progressImage PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
@param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
@return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
*/
- (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
options:(PINRemoteImageManagerDownloadOptions)options
progressImage:(nullable PINRemoteImageManagerImageCompletion)progressImage
completion:(nullable PINRemoteImageManagerImageCompletion)completion;

/**
Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
@param url NSURL where the image to download resides.
@param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
@param progressDownload PINRemoteImageManagerDownloadProgress block which will be called to update progress in bytes of the image download. NOTE: For performance reasons, this block is not called on the main thread every time, if you need to update your UI ensure that you dispatch to the main thread first.
@param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
@return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
*/
- (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
options:(PINRemoteImageManagerDownloadOptions)options
progress:(nullable PINRemoteImageManagerImageCompletion)progress
progressDownload:(nullable PINRemoteImageManagerProgressDownload)progressDownload
completion:(nullable PINRemoteImageManagerImageCompletion)completion;

/**
Download or retrieve from cache the image found at the url. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
@param url NSURL where the image to download resides.
@param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
@param progressImage PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
@param progressDownload PINRemoteImageManagerDownloadProgress block which will be called to update progress in bytes of the image download. NOTE: For performance reasons, this block is not called on the main thread every time, if you need to update your UI ensure that you dispatch to the main thread first.
@param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
@return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
*/
- (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
options:(PINRemoteImageManagerDownloadOptions)options
progressImage:(nullable PINRemoteImageManagerImageCompletion)progressImage
progressDownload:(nullable PINRemoteImageManagerProgressDownload)progressDownload
completion:(nullable PINRemoteImageManagerImageCompletion)completion;

/**
Download or retrieve from cache the image found at the url and process it before calling completion. All completions are called on an arbitrary callback queue unless called on the main thread and the result is in the memory cache (this is an optimization to allow synchronous results for the UI when an object is cached in memory).
@param url NSURL where the image to download resides.
@param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
@param processorKey NSString key to uniquely identify processor and process. Will be used for caching processed images.
@param processor PINRemoteImageManagerImageProcessor block which will be called to post-process downloaded image.
@param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
@return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
*/
- (nullable NSUUID *)downloadImageWithURL:(nonnull NSURL *)url
options:(PINRemoteImageManagerDownloadOptions)options
processorKey:(nullable NSString *)processorKey
processor:(nullable PINRemoteImageManagerImageProcessor)processor
completion:(nullable PINRemoteImageManagerImageCompletion)completion;

/**
Expand All @@ -308,6 +365,7 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _
@param url NSURL where the image to download resides.
@param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
@param processorKey NSString key to uniquely identify processor and process. Will be used for caching processed images.
@param progressDownload PINRemoteImageManagerDownloadProgress block which will be called to update progress in bytes of the image download. NOTE: For performance reasons, this block is not called on the main thread every time, if you need to update your UI ensure that you dispatch to the main thread first.
@param processor PINRemoteImageManagerImageProcessor block which will be called to post-process downloaded image.
@param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
Expand All @@ -317,6 +375,7 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _
options:(PINRemoteImageManagerDownloadOptions)options
processorKey:(nullable NSString *)processorKey
processor:(nullable PINRemoteImageManagerImageProcessor)processor
progressDownload:(nullable PINRemoteImageManagerProgressDownload)progressDownload
completion:(nullable PINRemoteImageManagerImageCompletion)completion;

/**
Expand All @@ -326,14 +385,14 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _
@param urls An array of NSURLs of increasing size.
@param options PINRemoteImageManagerDownloadOptions options with which to fetch the image.
@param progress PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
@param progressImage PINRemoteImageManagerImageCompletion block which will be called to update progress of the image download.
@param completion PINRemoteImageManagerImageCompletion block to call when image has been fetched from the cache or downloaded.
@return An NSUUID which uniquely identifies this request. To be used for canceling requests and verifying that the callback is for the request you expect (see categories for example).
*/
- (nullable NSUUID *)downloadImageWithURLs:(nonnull NSArray <NSURL *> *)urls
options:(PINRemoteImageManagerDownloadOptions)options
progress:(nullable PINRemoteImageManagerImageCompletion)progress
progressImage:(nullable PINRemoteImageManagerImageCompletion)progressImage
completion:(nullable PINRemoteImageManagerImageCompletion)completion;

/**
Expand Down Expand Up @@ -375,9 +434,9 @@ typedef void(^PINRemoteImageManagerAuthenticationChallenge)(NSURLSessionTask * _
/**
* @abstract set the progress callback on a download task. You can use this to add progress callbacks or remove them for in flight downloads
*
* @param progressCallback a PINRemoteImageManagerImageCompletion block to be called with a progress update
* @param progressImageCallback a PINRemoteImageManagerImageCompletion block to be called with a progress update
* @param UUID NSUUID of the task to set the priority on.
*/
- (void)setProgressCallback:(nullable PINRemoteImageManagerImageCompletion)progress ofTaskWithUUID:(nonnull NSUUID *)UUID;
- (void)setProgressImageCallback:(nullable PINRemoteImageManagerImageCompletion)progressImageBlock ofTaskWithUUID:(nonnull NSUUID *)UUID;

@end

0 comments on commit 2d7b0f9

Please sign in to comment.