Blame view

Pods/AFNetworking/UIKit+AFNetworking/AFImageDownloader.m 18.6 KB
75d24c15   yangbin   123
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
  // AFImageDownloader.m
  // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  //
  // Permission is hereby granted, free of charge, to any person obtaining a copy
  // of this software and associated documentation files (the "Software"), to deal
  // in the Software without restriction, including without limitation the rights
  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  // copies of the Software, and to permit persons to whom the Software is
  // furnished to do so, subject to the following conditions:
  //
  // The above copyright notice and this permission notice shall be included in
  // all copies or substantial portions of the Software.
  //
  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  // THE SOFTWARE.
  
  #import <TargetConditionals.h>
  
  #if TARGET_OS_IOS || TARGET_OS_TV
  
  #import "AFImageDownloader.h"
  #import "AFHTTPSessionManager.h"
  
  @interface AFImageDownloaderResponseHandler : NSObject
  @property (nonatomic, strong) NSUUID *uuid;
  @property (nonatomic, copy) void (^successBlock)(NSURLRequest *, NSHTTPURLResponse *, UIImage *);
  @property (nonatomic, copy) void (^failureBlock)(NSURLRequest *, NSHTTPURLResponse *, NSError *);
  @end
  
  @implementation AFImageDownloaderResponseHandler
  
  - (instancetype)initWithUUID:(NSUUID *)uuid
                       success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
                       failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure {
      if (self = [self init]) {
          self.uuid = uuid;
          self.successBlock = success;
          self.failureBlock = failure;
      }
      return self;
  }
  
  - (NSString *)description {
      return [NSString stringWithFormat: @"<AFImageDownloaderResponseHandler>UUID: %@", [self.uuid UUIDString]];
  }
  
  @end
  
  @interface AFImageDownloaderMergedTask : NSObject
  @property (nonatomic, strong) NSString *URLIdentifier;
  @property (nonatomic, strong) NSUUID *identifier;
  @property (nonatomic, strong) NSURLSessionDataTask *task;
  @property (nonatomic, strong) NSMutableArray <AFImageDownloaderResponseHandler*> *responseHandlers;
  
  @end
  
  @implementation AFImageDownloaderMergedTask
  
  - (instancetype)initWithURLIdentifier:(NSString *)URLIdentifier identifier:(NSUUID *)identifier task:(NSURLSessionDataTask *)task {
      if (self = [self init]) {
          self.URLIdentifier = URLIdentifier;
          self.task = task;
          self.identifier = identifier;
          self.responseHandlers = [[NSMutableArray alloc] init];
      }
      return self;
  }
  
  - (void)addResponseHandler:(AFImageDownloaderResponseHandler *)handler {
      [self.responseHandlers addObject:handler];
  }
  
  - (void)removeResponseHandler:(AFImageDownloaderResponseHandler *)handler {
      [self.responseHandlers removeObject:handler];
  }
  
  @end
  
  @implementation AFImageDownloadReceipt
  
  - (instancetype)initWithReceiptID:(NSUUID *)receiptID task:(NSURLSessionDataTask *)task {
      if (self = [self init]) {
          self.receiptID = receiptID;
          self.task = task;
      }
      return self;
  }
  
  @end
  
  @interface AFImageDownloader ()
  
  @property (nonatomic, strong) dispatch_queue_t synchronizationQueue;
  @property (nonatomic, strong) dispatch_queue_t responseQueue;
  
  @property (nonatomic, assign) NSInteger maximumActiveDownloads;
  @property (nonatomic, assign) NSInteger activeRequestCount;
  
  @property (nonatomic, strong) NSMutableArray *queuedMergedTasks;
  @property (nonatomic, strong) NSMutableDictionary *mergedTasks;
  
  @end
  
  @implementation AFImageDownloader
  
  + (NSURLCache *)defaultURLCache {
      NSUInteger memoryCapacity = 20 * 1024 * 1024; // 20MB
      NSUInteger diskCapacity = 150 * 1024 * 1024; // 150MB
      NSURL *cacheURL = [[[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory
                                                                inDomain:NSUserDomainMask
                                                       appropriateForURL:nil
                                                                  create:YES
                                                                   error:nil]
                         URLByAppendingPathComponent:@"com.alamofire.imagedownloader"];
      
  #if TARGET_OS_MACCATALYST
      return [[NSURLCache alloc] initWithMemoryCapacity:memoryCapacity
                                           diskCapacity:diskCapacity
                                           directoryURL:cacheURL];
  #else
      return [[NSURLCache alloc] initWithMemoryCapacity:memoryCapacity
                                           diskCapacity:diskCapacity
                                               diskPath:[cacheURL path]];
  #endif
  }
  
  + (NSURLSessionConfiguration *)defaultURLSessionConfiguration {
      NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  
      //TODO set the default HTTP headers
  
      configuration.HTTPShouldSetCookies = YES;
      configuration.HTTPShouldUsePipelining = NO;
  
      configuration.requestCachePolicy = NSURLRequestUseProtocolCachePolicy;
      configuration.allowsCellularAccess = YES;
      configuration.timeoutIntervalForRequest = 60.0;
      configuration.URLCache = [AFImageDownloader defaultURLCache];
  
      return configuration;
  }
  
  - (instancetype)init {
      NSURLSessionConfiguration *defaultConfiguration = [self.class defaultURLSessionConfiguration];
      return [self initWithSessionConfiguration:defaultConfiguration];
  }
  
  - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
      AFHTTPSessionManager *sessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:configuration];
      sessionManager.responseSerializer = [AFImageResponseSerializer serializer];
  
      return [self initWithSessionManager:sessionManager
                   downloadPrioritization:AFImageDownloadPrioritizationFIFO
                   maximumActiveDownloads:4
                               imageCache:[[AFAutoPurgingImageCache alloc] init]];
  }
  
  - (instancetype)initWithSessionManager:(AFHTTPSessionManager *)sessionManager
                  downloadPrioritization:(AFImageDownloadPrioritization)downloadPrioritization
                  maximumActiveDownloads:(NSInteger)maximumActiveDownloads
                              imageCache:(id <AFImageRequestCache>)imageCache {
      if (self = [super init]) {
          self.sessionManager = sessionManager;
  
          self.downloadPrioritization = downloadPrioritization;
          self.maximumActiveDownloads = maximumActiveDownloads;
          self.imageCache = imageCache;
  
          self.queuedMergedTasks = [[NSMutableArray alloc] init];
          self.mergedTasks = [[NSMutableDictionary alloc] init];
          self.activeRequestCount = 0;
  
          NSString *name = [NSString stringWithFormat:@"com.alamofire.imagedownloader.synchronizationqueue-%@", [[NSUUID UUID] UUIDString]];
          self.synchronizationQueue = dispatch_queue_create([name cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_SERIAL);
  
          name = [NSString stringWithFormat:@"com.alamofire.imagedownloader.responsequeue-%@", [[NSUUID UUID] UUIDString]];
          self.responseQueue = dispatch_queue_create([name cStringUsingEncoding:NSASCIIStringEncoding], DISPATCH_QUEUE_CONCURRENT);
      }
  
      return self;
  }
  
  + (instancetype)defaultInstance {
      static AFImageDownloader *sharedInstance = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&onceToken, ^{
          sharedInstance = [[self alloc] init];
      });
      return sharedInstance;
  }
  
  - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
                                                          success:(void (^)(NSURLRequest * _Nonnull, NSHTTPURLResponse * _Nullable, UIImage * _Nonnull))success
                                                          failure:(void (^)(NSURLRequest * _Nonnull, NSHTTPURLResponse * _Nullable, NSError * _Nonnull))failure {
      return [self downloadImageForURLRequest:request withReceiptID:[NSUUID UUID] success:success failure:failure];
  }
  
  - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
                                                    withReceiptID:(nonnull NSUUID *)receiptID
                                                          success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse  * _Nullable response, UIImage *responseObject))success
                                                          failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure {
      __block NSURLSessionDataTask *task = nil;
      dispatch_sync(self.synchronizationQueue, ^{
          NSString *URLIdentifier = request.URL.absoluteString;
          if (URLIdentifier == nil) {
              if (failure) {
                  NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadURL userInfo:nil];
                  dispatch_async(dispatch_get_main_queue(), ^{
                      failure(request, nil, error);
                  });
              }
              return;
          }
  
          // 1) Append the success and failure blocks to a pre-existing request if it already exists
          AFImageDownloaderMergedTask *existingMergedTask = self.mergedTasks[URLIdentifier];
          if (existingMergedTask != nil) {
              AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID success:success failure:failure];
              [existingMergedTask addResponseHandler:handler];
              task = existingMergedTask.task;
              return;
          }
  
          // 2) Attempt to load the image from the image cache if the cache policy allows it
          switch (request.cachePolicy) {
              case NSURLRequestUseProtocolCachePolicy:
              case NSURLRequestReturnCacheDataElseLoad:
              case NSURLRequestReturnCacheDataDontLoad: {
                  UIImage *cachedImage = [self.imageCache imageforRequest:request withAdditionalIdentifier:nil];
                  if (cachedImage != nil) {
                      if (success) {
                          dispatch_async(dispatch_get_main_queue(), ^{
                              success(request, nil, cachedImage);
                          });
                      }
                      return;
                  }
                  break;
              }
              default:
                  break;
          }
  
          // 3) Create the request and set up authentication, validation and response serialization
          NSUUID *mergedTaskIdentifier = [NSUUID UUID];
          NSURLSessionDataTask *createdTask;
          __weak __typeof__(self) weakSelf = self;
  
          createdTask = [self.sessionManager
                         dataTaskWithRequest:request
                         uploadProgress:nil
                         downloadProgress:nil
                         completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
                             dispatch_async(self.responseQueue, ^{
                                 __strong __typeof__(weakSelf) strongSelf = weakSelf;
                                 AFImageDownloaderMergedTask *mergedTask = [strongSelf safelyGetMergedTask:URLIdentifier];
                                 if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) {
                                     mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier];
                                     if (error) {
                                         for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
                                             if (handler.failureBlock) {
                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     handler.failureBlock(request, (NSHTTPURLResponse *)response, error);
                                                 });
                                             }
                                         }
                                     } else {
                                         if ([strongSelf.imageCache shouldCacheImage:responseObject forRequest:request withAdditionalIdentifier:nil]) {
                                             [strongSelf.imageCache addImage:responseObject forRequest:request withAdditionalIdentifier:nil];
                                         }
  
                                         for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
                                             if (handler.successBlock) {
                                                 dispatch_async(dispatch_get_main_queue(), ^{
                                                     handler.successBlock(request, (NSHTTPURLResponse *)response, responseObject);
                                                 });
                                             }
                                         }
                                         
                                     }
                                 }
                                 [strongSelf safelyDecrementActiveTaskCount];
                                 [strongSelf safelyStartNextTaskIfNecessary];
                             });
                         }];
  
          // 4) Store the response handler for use when the request completes
          AFImageDownloaderResponseHandler *handler = [[AFImageDownloaderResponseHandler alloc] initWithUUID:receiptID
                                                                                                     success:success
                                                                                                     failure:failure];
          AFImageDownloaderMergedTask *mergedTask = [[AFImageDownloaderMergedTask alloc]
                                                     initWithURLIdentifier:URLIdentifier
                                                     identifier:mergedTaskIdentifier
                                                     task:createdTask];
          [mergedTask addResponseHandler:handler];
          self.mergedTasks[URLIdentifier] = mergedTask;
  
          // 5) Either start the request or enqueue it depending on the current active request count
          if ([self isActiveRequestCountBelowMaximumLimit]) {
              [self startMergedTask:mergedTask];
          } else {
              [self enqueueMergedTask:mergedTask];
          }
  
          task = mergedTask.task;
      });
      if (task) {
          return [[AFImageDownloadReceipt alloc] initWithReceiptID:receiptID task:task];
      } else {
          return nil;
      }
  }
  
  - (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt {
      dispatch_sync(self.synchronizationQueue, ^{
          NSString *URLIdentifier = imageDownloadReceipt.task.originalRequest.URL.absoluteString;
          AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier];
          NSUInteger index = [mergedTask.responseHandlers indexOfObjectPassingTest:^BOOL(AFImageDownloaderResponseHandler * _Nonnull handler, __unused NSUInteger idx, __unused BOOL * _Nonnull stop) {
              return handler.uuid == imageDownloadReceipt.receiptID;
          }];
  
          if (index != NSNotFound) {
              AFImageDownloaderResponseHandler *handler = mergedTask.responseHandlers[index];
              [mergedTask removeResponseHandler:handler];
              NSString *failureReason = [NSString stringWithFormat:@"ImageDownloader cancelled URL request: %@",imageDownloadReceipt.task.originalRequest.URL.absoluteString];
              NSDictionary *userInfo = @{NSLocalizedFailureReasonErrorKey:failureReason};
              NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:userInfo];
              if (handler.failureBlock) {
                  dispatch_async(dispatch_get_main_queue(), ^{
                      handler.failureBlock(imageDownloadReceipt.task.originalRequest, nil, error);
                  });
              }
          }
  
          if (mergedTask.responseHandlers.count == 0) {
              [mergedTask.task cancel];
              [self removeMergedTaskWithURLIdentifier:URLIdentifier];
          }
      });
  }
  
  - (AFImageDownloaderMergedTask *)safelyRemoveMergedTaskWithURLIdentifier:(NSString *)URLIdentifier {
      __block AFImageDownloaderMergedTask *mergedTask = nil;
      dispatch_sync(self.synchronizationQueue, ^{
          mergedTask = [self removeMergedTaskWithURLIdentifier:URLIdentifier];
      });
      return mergedTask;
  }
  
  //This method should only be called from safely within the synchronizationQueue
  - (AFImageDownloaderMergedTask *)removeMergedTaskWithURLIdentifier:(NSString *)URLIdentifier {
      AFImageDownloaderMergedTask *mergedTask = self.mergedTasks[URLIdentifier];
      [self.mergedTasks removeObjectForKey:URLIdentifier];
      return mergedTask;
  }
  
  - (void)safelyDecrementActiveTaskCount {
      dispatch_sync(self.synchronizationQueue, ^{
          if (self.activeRequestCount > 0) {
              self.activeRequestCount -= 1;
          }
      });
  }
  
  - (void)safelyStartNextTaskIfNecessary {
      dispatch_sync(self.synchronizationQueue, ^{
          if ([self isActiveRequestCountBelowMaximumLimit]) {
              while (self.queuedMergedTasks.count > 0) {
                  AFImageDownloaderMergedTask *mergedTask = [self dequeueMergedTask];
                  if (mergedTask.task.state == NSURLSessionTaskStateSuspended) {
                      [self startMergedTask:mergedTask];
                      break;
                  }
              }
          }
      });
  }
  
  - (void)startMergedTask:(AFImageDownloaderMergedTask *)mergedTask {
      [mergedTask.task resume];
      ++self.activeRequestCount;
  }
  
  - (void)enqueueMergedTask:(AFImageDownloaderMergedTask *)mergedTask {
      switch (self.downloadPrioritization) {
          case AFImageDownloadPrioritizationFIFO:
              [self.queuedMergedTasks addObject:mergedTask];
              break;
          case AFImageDownloadPrioritizationLIFO:
              [self.queuedMergedTasks insertObject:mergedTask atIndex:0];
              break;
      }
  }
  
  - (AFImageDownloaderMergedTask *)dequeueMergedTask {
      AFImageDownloaderMergedTask *mergedTask = nil;
      mergedTask = [self.queuedMergedTasks firstObject];
      [self.queuedMergedTasks removeObject:mergedTask];
      return mergedTask;
  }
  
  - (BOOL)isActiveRequestCountBelowMaximumLimit {
      return self.activeRequestCount < self.maximumActiveDownloads;
  }
  
  - (AFImageDownloaderMergedTask *)safelyGetMergedTask:(NSString *)URLIdentifier {
      __block AFImageDownloaderMergedTask *mergedTask;
      dispatch_sync(self.synchronizationQueue, ^(){
          mergedTask = self.mergedTasks[URLIdentifier];
      });
      return mergedTask;
  }
  
  @end
  
  #endif