HUWebImageDownloadOperation.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // HUWebImageDownloadOperation.m
  3. // Pods
  4. //
  5. // Created by mac on 16/4/19.
  6. //
  7. //
  8. #import "HUWebImageDownloadOperation.h"
  9. #import "UIImage+HUExtension.h"
  10. @interface HUWebImageDownloadOperation () {
  11. NSLock *_lock;
  12. }
  13. @property (nonatomic, copy) HUWebImageDownloadCompltedBlock completedBlock;
  14. @property (nonatomic, strong) NSURL *url;
  15. @property (nonatomic, strong) NSURLSession *session;
  16. @property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
  17. @property (nonatomic, assign) BOOL myFinished;
  18. @property (nonatomic, assign) BOOL myExecuting;
  19. @end
  20. @implementation HUWebImageDownloadOperation
  21. - (instancetype)initWithURL:(NSURL *)url completed:(HUWebImageDownloadCompltedBlock)completedBlock {
  22. if (self = [super init]) {
  23. _url = url;
  24. _completedBlock = completedBlock;
  25. _lock = [NSLock new];
  26. }
  27. return self;
  28. }
  29. - (void)resume {
  30. if (self.isCancelled && !self.isFinished) {
  31. [_lock lock];
  32. self.myExecuting = YES;
  33. [_lock unlock];
  34. [_downloadTask resume];
  35. }
  36. }
  37. - (void)start {
  38. if (self.isCancelled) {
  39. [self reset];
  40. return;
  41. }
  42. [_lock lock];
  43. self.myExecuting = YES;
  44. [_lock unlock];
  45. [self beginTask];
  46. }
  47. - (BOOL)isConcurrent {
  48. return YES;
  49. }
  50. - (BOOL)isFinished {
  51. return _myFinished;
  52. }
  53. - (BOOL)isExecuting {
  54. return _myExecuting;
  55. }
  56. - (void)setMyExecuting:(BOOL)myExecuting {
  57. [self willChangeValueForKey:@"isExecuting"];
  58. _myExecuting = myExecuting;
  59. [self didChangeValueForKey:@"isExecuting"];
  60. }
  61. - (void)setMyFinished:(BOOL)myFinished {
  62. [self willChangeValueForKey:@"isFinished"];
  63. _myFinished = myFinished;
  64. [self didChangeValueForKey:@"isFinished"];
  65. }
  66. - (void)beginTask {
  67. NSURLRequest *request = [NSURLRequest requestWithURL:_url];
  68. __weak __typeof(self) wself = self;
  69. self.downloadTask = [self.session downloadTaskWithRequest:request completionHandler:^(NSURL * _Nullable location, NSURLResponse * _Nullable response, NSError * _Nullable error) {
  70. __strong __typeof(self) sself = wself;
  71. [sself end];
  72. if (!sself.completedBlock) {
  73. sself.myFinished = YES;
  74. return ;
  75. }
  76. NSString *caches = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES) lastObject];
  77. NSString *file = [caches stringByAppendingPathComponent:response.suggestedFilename];
  78. if (location.path) {
  79. [NSFileManager.defaultManager moveItemAtPath:location.path toPath:file error:nil];
  80. }
  81. NSData *data = [NSData dataWithContentsOfFile:file];
  82. [NSFileManager.defaultManager removeItemAtPath:file error:nil];
  83. if (data == nil || sself.isCancelled) {
  84. sself.completedBlock(nil, nil, error);
  85. sself.myFinished = YES;
  86. return ;
  87. }
  88. UIImage *image = [UIImage hu_imageFromData:data];
  89. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  90. sself.completedBlock(image, data, nil);
  91. sself.myFinished = YES;
  92. }];
  93. }];
  94. [_downloadTask resume];
  95. }
  96. - (void)reset {
  97. [self end];
  98. self.completedBlock = nil;
  99. [[self.session dataTaskWithURL:_url] cancel];
  100. }
  101. - (void)end {
  102. self.myExecuting = NO;
  103. self.myFinished = YES;
  104. }
  105. - (NSURLSession *)session {
  106. if (_session == nil) {
  107. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  108. _session = [NSURLSession sessionWithConfiguration:configuration];
  109. }
  110. return _session;
  111. }
  112. @end