PINRemoteImageDownloadTask.m 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // PINRemoteImageDownloadTask.m
  3. // Pods
  4. //
  5. // Created by Garrett Moon on 3/9/15.
  6. //
  7. //
  8. #import "PINRemoteImageDownloadTask.h"
  9. #import "PINRemoteImage.h"
  10. #import "PINRemoteImageCallbacks.h"
  11. @interface PINRemoteImageDownloadTask ()
  12. @end
  13. @implementation PINRemoteImageDownloadTask
  14. - (instancetype)init
  15. {
  16. if (self = [super init]) {
  17. _numberOfRetries = 0;
  18. }
  19. return self;
  20. }
  21. - (BOOL)hasProgressBlocks
  22. {
  23. __block BOOL hasProgressBlocks = NO;
  24. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  25. if (callback.progressImageBlock) {
  26. hasProgressBlocks = YES;
  27. *stop = YES;
  28. }
  29. }];
  30. return hasProgressBlocks;
  31. }
  32. - (void)callProgressDownloadWithQueue:(nonnull dispatch_queue_t)queue completedBytes:(int64_t)completedBytes totalBytes:(int64_t)totalBytes
  33. {
  34. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  35. if (callback.progressDownloadBlock != nil) {
  36. PINLog(@"calling progress for UUID: %@ key: %@", UUID, self.key);
  37. PINRemoteImageManagerProgressDownload progressDownloadBlock = callback.progressDownloadBlock;
  38. //The code run asynchronously below is *not* guaranteed to be run in the manager's lock!
  39. //All access to the callbacks and self should be done outside the block below!
  40. dispatch_async(queue, ^
  41. {
  42. progressDownloadBlock(completedBytes, totalBytes);
  43. });
  44. }
  45. }];
  46. }
  47. - (void)callProgressImageWithQueue:(nonnull dispatch_queue_t)queue withImage:(nonnull PINImage *)image renderedImageQuality:(CGFloat)renderedImageQuality
  48. {
  49. [self.callbackBlocks enumerateKeysAndObjectsUsingBlock:^(NSUUID *UUID, PINRemoteImageCallbacks *callback, BOOL *stop) {
  50. if (callback.progressImageBlock != nil) {
  51. PINLog(@"calling progress for UUID: %@ key: %@", UUID, self.key);
  52. PINRemoteImageManagerImageCompletion progressImageBlock = callback.progressImageBlock;
  53. CFTimeInterval requestTime = callback.requestTime;
  54. //The code run asynchronously below is *not* guaranteed to be run in the manager's lock!
  55. //All access to the callbacks and self should be done outside the block below!
  56. dispatch_async(queue, ^
  57. {
  58. progressImageBlock([PINRemoteImageManagerResult imageResultWithImage:image
  59. alternativeRepresentation:nil
  60. requestLength:CACurrentMediaTime() - requestTime
  61. error:nil
  62. resultType:PINRemoteImageResultTypeProgress
  63. UUID:UUID
  64. renderedImageQuality:renderedImageQuality]);
  65. });
  66. }
  67. }];
  68. }
  69. - (BOOL)cancelWithUUID:(NSUUID *)UUID manager:(PINRemoteImageManager *)manager
  70. {
  71. BOOL noMoreCompletions = [super cancelWithUUID:UUID manager:manager];
  72. if (noMoreCompletions) {
  73. [self.urlSessionTaskOperation cancel];
  74. PINLog(@"Canceling download of URL: %@, UUID: %@", self.urlSessionTaskOperation.dataTask.originalRequest.URL, UUID);
  75. } else {
  76. PINLog(@"Decrementing download of URL: %@, UUID: %@", self.urlSessionTaskOperation.dataTask.originalRequest.URL, UUID);
  77. }
  78. return noMoreCompletions;
  79. }
  80. - (void)setPriority:(PINRemoteImageManagerPriority)priority
  81. {
  82. [super setPriority:priority];
  83. if (PINNSURLSessionTaskSupportsPriority) {
  84. self.urlSessionTaskOperation.dataTask.priority = dataTaskPriorityWithImageManagerPriority(priority);
  85. }
  86. self.urlSessionTaskOperation.queuePriority = operationPriorityWithImageManagerPriority(priority);
  87. }
  88. @end