暫無描述

AFImageDownloader.h 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 downloadPrioritizaton;
  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. Default initializer
  70. @return An instance of `AFImageDownloader` initialized with default values.
  71. */
  72. - (instancetype)init;
  73. /**
  74. Initializes the `AFImageDownloader` instance with the given session manager, download prioritization, maximum active download count and image cache.
  75. @param sessionManager The session manager to use to download images.
  76. @param downloadPrioritization The download prioritization of the download queue.
  77. @param maximumActiveDownloads The maximum number of active downloads allowed at any given time. Recommend `4`.
  78. @param imageCache The image cache used to store all downloaded images in.
  79. @return The new `AFImageDownloader` instance.
  80. */
  81. - (instancetype)initWithSessionManager:(AFHTTPSessionManager *)sessionManager
  82. downloadPrioritization:(AFImageDownloadPrioritization)downloadPrioritization
  83. maximumActiveDownloads:(NSInteger)maximumActiveDownloads
  84. imageCache:(nullable id <AFImageRequestCache>)imageCache;
  85. /**
  86. Creates a data task using the `sessionManager` instance for the specified URL request.
  87. If the same data task is already in the queue or currently being downloaded, the success and failure blocks are
  88. appended to the already existing task. Once the task completes, all success or failure blocks attached to the
  89. task are executed in the order they were added.
  90. @param request The URL request.
  91. @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`.
  92. @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.
  93. @return The image download receipt for the data task if available. `nil` if the image is stored in the cache.
  94. cache and the URL request cache policy allows the cache to be used.
  95. */
  96. - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
  97. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
  98. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
  99. /**
  100. Creates a data task using the `sessionManager` instance for the specified URL request.
  101. If the same data task is already in the queue or currently being downloaded, the success and failure blocks are
  102. appended to the already existing task. Once the task completes, all success or failure blocks attached to the
  103. task are executed in the order they were added.
  104. @param request The URL request.
  105. @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.
  106. @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`.
  107. @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.
  108. @return The image download receipt for the data task if available. `nil` if the image is stored in the cache.
  109. cache and the URL request cache policy allows the cache to be used.
  110. */
  111. - (nullable AFImageDownloadReceipt *)downloadImageForURLRequest:(NSURLRequest *)request
  112. withReceiptID:(NSUUID *)receiptID
  113. success:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *responseObject))success
  114. failure:(nullable void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure;
  115. /**
  116. Cancels the data task in the receipt by removing the corresponding success and failure blocks and cancelling the data task if necessary.
  117. 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.
  118. @param imageDownloadReceipt The image download receipt to cancel.
  119. */
  120. - (void)cancelTaskForImageDownloadReceipt:(AFImageDownloadReceipt *)imageDownloadReceipt;
  121. @end
  122. #endif
  123. NS_ASSUME_NONNULL_END