AFImageDownloader.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // AFImageDownloader.h
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import <TargetConditionals.h>
  22. #if TARGET_OS_IOS || TARGET_OS_TV
  23. #import <Foundation/Foundation.h>
  24. #import "AFAutoPurgingImageCache.h"
  25. #import "AFHTTPSessionManager.h"
  26. NS_ASSUME_NONNULL_BEGIN
  27. typedef NS_ENUM(NSInteger, AFImageDownloadPrioritization) {
  28. AFImageDownloadPrioritizationFIFO,
  29. AFImageDownloadPrioritizationLIFO
  30. };
  31. /**
  32. The `AFImageDownloadReceipt` is an object vended by the `AFImageDownloader` when starting a data task. It can be used to cancel active tasks running on the `AFImageDownloader` session. As a general rule, image data tasks should be cancelled using the `AFImageDownloadReceipt` instead of calling `cancel` directly on the `task` itself. The `AFImageDownloader` is optimized to handle duplicate task scenarios as well as pending versus active downloads.
  33. */
  34. @interface AFImageDownloadReceipt : NSObject
  35. /**
  36. The data task created by the `AFImageDownloader`.
  37. */
  38. @property (nonatomic, strong) NSURLSessionDataTask *task;
  39. /**
  40. The unique identifier for the success and failure blocks when duplicate requests are made.
  41. */
  42. @property (nonatomic, strong) NSUUID *receiptID;
  43. @end
  44. /** The `AFImageDownloader` class is responsible for downloading images in parallel on a prioritized queue. Incoming downloads are added to the front or back of the queue depending on the download prioritization. Each downloaded image is cached in the underlying `NSURLCache` as well as the in-memory image cache. By default, any download request with a cached image equivalent in the image cache will automatically be served the cached image representation.
  45. */
  46. @interface AFImageDownloader : NSObject
  47. /**
  48. The image cache used to store all downloaded images in. `AFAutoPurgingImageCache` by default.
  49. */
  50. @property (nonatomic, strong, nullable) id <AFImageRequestCache> imageCache;
  51. /**
  52. The `AFHTTPSessionManager` used to download images. By default, this is configured with an `AFImageResponseSerializer`, and a shared `NSURLCache` for all image downloads.
  53. */
  54. @property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
  55. /**
  56. Defines the order prioritization of incoming download requests being inserted into the queue. `AFImageDownloadPrioritizationFIFO` by default.
  57. */
  58. @property (nonatomic, assign) AFImageDownloadPrioritization downloadPrioritization;
  59. /**
  60. The shared default instance of `AFImageDownloader` initialized with default values.
  61. */
  62. + (instancetype)defaultInstance;
  63. /**
  64. Creates a default `NSURLCache` with common usage parameter values.
  65. @returns The default `NSURLCache` instance.
  66. */
  67. + (NSURLCache *)defaultURLCache;
  68. /**
  69. The default `NSURLSessionConfiguration` with common usage parameter values.
  70. */
  71. + (NSURLSessionConfiguration *)defaultURLSessionConfiguration;
  72. /**
  73. Default initializer
  74. @return An instance of `AFImageDownloader` initialized with default values.
  75. */
  76. - (instancetype)init;
  77. /**
  78. Initializer with specific `URLSessionConfiguration`
  79. @param configuration The `NSURLSessionConfiguration` to be be used
  80. @return An instance of `AFImageDownloader` initialized with default values and custom `NSURLSessionConfiguration`
  81. */
  82. - (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration;
  83. /**
  84. Initializes the `AFImageDownloader` instance with the given session manager, download prioritization, maximum active download count and image cache.
  85. @param sessionManager The session manager to use to download images.
  86. @param downloadPrioritization The download prioritization of the download queue.
  87. @param maximumActiveDownloads The maximum number of active downloads allowed at any given time. Recommend `4`.
  88. @param imageCache The image cache used to store all downloaded images in.
  89. @return The new `AFImageDownloader` instance.
  90. */
  91. - (instancetype)initWithSessionManager:(AFHTTPSessionManager *)sessionManager
  92. downloadPrioritization:(AFImageDownloadPrioritization)downloadPrioritization
  93. maximumActiveDownloads:(NSInteger)maximumActiveDownloads
  94. imageCache:(nullable id <AFImageRequestCache>)imageCache;
  95. /**
  96. Creates a data task using the `sessionManager` instance for the specified URL request.
  97. If the same data task is already in the queue or currently being downloaded, the success and failure blocks are
  98. appended to the already existing task. Once the task completes, all success or failure blocks attached to the
  99. task are executed in the order they were added.
  100. @param request The URL request.
  101. @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`.
  102. @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred.
  103. @return The image download receipt for the data task if available. `nil` if the image is stored in the cache.
  104. cache and the URL request cache policy allows the cache to be used.
  105. */
  106. - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
  107. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
  108. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
  109. /**
  110. Creates a data task using the `sessionManager` instance for the specified URL request.
  111. If the same data task is already in the queue or currently being downloaded, the success and failure blocks are
  112. appended to the already existing task. Once the task completes, all success or failure blocks attached to the
  113. task are executed in the order they were added.
  114. @param request The URL request.
  115. @param receiptID The identifier to use for the download receipt that will be created for this request. This must be a unique identifier that does not represent any other request.
  116. @param success A block to be executed when the image data task finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the image created from the response data of request. If the image was returned from cache, the response parameter will be `nil`.
  117. @param failure A block object to be executed when the image data task finishes unsuccessfully, or that finishes successfully. This block has no return value and takes three arguments: the request sent from the client, the response received from the server, and the error object describing the network or parsing error that occurred.
  118. @return The image download receipt for the data task if available. `nil` if the image is stored in the cache.
  119. cache and the URL request cache policy allows the cache to be used.
  120. */
  121. - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
  122. withReceiptID:(NSUUID *)receiptID
  123. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
  124. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
  125. /**
  126. Cancels the data task in the receipt by removing the corresponding success and failure blocks and cancelling the data task if necessary.
  127. If the data task is pending in the queue, it will be cancelled if no other success and failure blocks are registered with the data task. If the data task is currently executing or is already completed, the success and failure blocks are removed and will not be called when the task finishes.
  128. @param imageDownloadReceipt The image download receipt to cancel.
  129. */
  130. - (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt;
  131. @end
  132. #endif
  133. NS_ASSUME_NONNULL_END