SDWebImageDownloader.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. *
  5. * For the full copyright and license information, please view the LICENSE
  6. * file that was distributed with this source code.
  7. */
  8. #import <Foundation/Foundation.h>
  9. #import "SDWebImageCompat.h"
  10. #import "SDWebImageOperation.h"
  11. typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
  12. /**
  13. * Put the download in the low queue priority and task priority.
  14. */
  15. SDWebImageDownloaderLowPriority = 1 << 0,
  16. /**
  17. * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
  18. */
  19. SDWebImageDownloaderProgressiveDownload = 1 << 1,
  20. /**
  21. * By default, request prevent the use of NSURLCache. With this flag, NSURLCache
  22. * is used with default policies.
  23. */
  24. SDWebImageDownloaderUseNSURLCache = 1 << 2,
  25. /**
  26. * Call completion block with nil image/imageData if the image was read from NSURLCache
  27. * (to be combined with `SDWebImageDownloaderUseNSURLCache`).
  28. */
  29. SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
  30. /**
  31. * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
  32. * extra time in background to let the request finish. If the background task expires the operation will be cancelled.
  33. */
  34. SDWebImageDownloaderContinueInBackground = 1 << 4,
  35. /**
  36. * Handles cookies stored in NSHTTPCookieStore by setting
  37. * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
  38. */
  39. SDWebImageDownloaderHandleCookies = 1 << 5,
  40. /**
  41. * Enable to allow untrusted SSL certificates.
  42. * Useful for testing purposes. Use with caution in production.
  43. */
  44. SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
  45. /**
  46. * Put the download in the high queue priority and task priority.
  47. */
  48. SDWebImageDownloaderHighPriority = 1 << 7,
  49. /**
  50. * Scale down the image
  51. */
  52. SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
  53. };
  54. typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
  55. /**
  56. * Default value. All download operations will execute in queue style (first-in-first-out).
  57. */
  58. SDWebImageDownloaderFIFOExecutionOrder,
  59. /**
  60. * All download operations will execute in stack style (last-in-first-out).
  61. */
  62. SDWebImageDownloaderLIFOExecutionOrder
  63. };
  64. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStartNotification;
  65. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStopNotification;
  66. typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
  67. typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
  68. typedef NSDictionary<NSString *, NSString *> SDHTTPHeadersDictionary;
  69. typedef NSMutableDictionary<NSString *, NSString *> SDHTTPHeadersMutableDictionary;
  70. typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterBlock)(NSURL * _Nullable url, SDHTTPHeadersDictionary * _Nullable headers);
  71. /**
  72. * A token associated with each download. Can be used to cancel a download
  73. */
  74. @interface SDWebImageDownloadToken : NSObject <SDWebImageOperation>
  75. /**
  76. The download's URL. This should be readonly and you should not modify
  77. */
  78. @property (nonatomic, strong, nullable) NSURL *url;
  79. /**
  80. The cancel token taken from `addHandlersForProgress:completed`. This should be readonly and you should not modify
  81. @note use `-[SDWebImageDownloadToken cancel]` to cancel the token
  82. */
  83. @property (nonatomic, strong, nullable) id downloadOperationCancelToken;
  84. @end
  85. /**
  86. * Asynchronous downloader dedicated and optimized for image loading.
  87. */
  88. @interface SDWebImageDownloader : NSObject
  89. /**
  90. * Decompressing images that are downloaded and cached can improve performance but can consume lot of memory.
  91. * Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
  92. */
  93. @property (assign, nonatomic) BOOL shouldDecompressImages;
  94. /**
  95. * The maximum number of concurrent downloads
  96. */
  97. @property (assign, nonatomic) NSInteger maxConcurrentDownloads;
  98. /**
  99. * Shows the current amount of downloads that still need to be downloaded
  100. */
  101. @property (readonly, nonatomic) NSUInteger currentDownloadCount;
  102. /**
  103. * The timeout value (in seconds) for the download operation. Default: 15.0.
  104. */
  105. @property (assign, nonatomic) NSTimeInterval downloadTimeout;
  106. /**
  107. * The configuration in use by the internal NSURLSession.
  108. * Mutating this object directly has no effect.
  109. *
  110. * @see createNewSessionWithConfiguration:
  111. */
  112. @property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration;
  113. /**
  114. * Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`.
  115. */
  116. @property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;
  117. /**
  118. * Singleton method, returns the shared instance
  119. *
  120. * @return global shared instance of downloader class
  121. */
  122. + (nonnull instancetype)sharedDownloader;
  123. /**
  124. * Set the default URL credential to be set for request operations.
  125. */
  126. @property (strong, nonatomic, nullable) NSURLCredential *urlCredential;
  127. /**
  128. * Set username
  129. */
  130. @property (strong, nonatomic, nullable) NSString *username;
  131. /**
  132. * Set password
  133. */
  134. @property (strong, nonatomic, nullable) NSString *password;
  135. /**
  136. * Set filter to pick headers for downloading image HTTP request.
  137. *
  138. * This block will be invoked for each downloading image request, returned
  139. * NSDictionary will be used as headers in corresponding HTTP request.
  140. */
  141. @property (nonatomic, copy, nullable) SDWebImageDownloaderHeadersFilterBlock headersFilter;
  142. /**
  143. * Creates an instance of a downloader with specified session configuration.
  144. * @note `timeoutIntervalForRequest` is going to be overwritten.
  145. * @return new instance of downloader class
  146. */
  147. - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration NS_DESIGNATED_INITIALIZER;
  148. /**
  149. * Set a value for a HTTP header to be appended to each download HTTP request.
  150. *
  151. * @param value The value for the header field. Use `nil` value to remove the header.
  152. * @param field The name of the header field to set.
  153. */
  154. - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field;
  155. /**
  156. * Returns the value of the specified HTTP header field.
  157. *
  158. * @return The value associated with the header field field, or `nil` if there is no corresponding header field.
  159. */
  160. - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field;
  161. /**
  162. * Sets a subclass of `NSOperation` and conforms to `SDWebImageDownloaderOperationInterface`.
  163. * Default is `SDWebImageDownloaderOperation`.
  164. * Can be used each time SDWebImage constructs a request
  165. * operation to download an image.
  166. *
  167. * @param operationClass The subclass of `NSOperation` and conforms to `SDWebImageDownloaderOperationInterface`.
  168. * Default is `SDWebImageDownloaderOperation`, Passing `nil` will revert to `SDWebImageDownloaderOperation`.
  169. */
  170. - (void)setOperationClass:(nullable Class)operationClass;
  171. /**
  172. * Creates a SDWebImageDownloader async downloader instance with a given URL
  173. *
  174. * The delegate will be informed when the image is finish downloaded or an error has happen.
  175. *
  176. * @see SDWebImageDownloaderDelegate
  177. *
  178. * @param url The URL to the image to download
  179. * @param completedBlock A block called once the download is completed.
  180. * If the download succeeded, the image parameter is set, in case of error,
  181. * error parameter is set with the error. The last parameter is always YES
  182. * if SDWebImageDownloaderProgressiveDownload isn't use. With the
  183. * SDWebImageDownloaderProgressiveDownload option, this block is called
  184. * repeatedly with the partial image object and the finished argument set to NO
  185. * before to be called a last time with the full image and finished argument
  186. * set to YES. In case of error, the finished argument is always YES.
  187. */
  188. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  189. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  190. /**
  191. * Creates a SDWebImageDownloader async downloader instance with a given URL
  192. *
  193. * The delegate will be informed when the image is finish downloaded or an error has happen.
  194. *
  195. * @see SDWebImageDownloaderDelegate
  196. *
  197. * @param url The URL to the image to download
  198. * @param options The options to be used for this download
  199. * @param progressBlock A block called repeatedly while the image is downloading
  200. * @note the progress block is executed on a background queue
  201. * @param completedBlock A block called once the download is completed.
  202. * If the download succeeded, the image parameter is set, in case of error,
  203. * error parameter is set with the error. The last parameter is always YES
  204. * if SDWebImageDownloaderProgressiveDownload isn't use. With the
  205. * SDWebImageDownloaderProgressiveDownload option, this block is called
  206. * repeatedly with the partial image object and the finished argument set to NO
  207. * before to be called a last time with the full image and finished argument
  208. * set to YES. In case of error, the finished argument is always YES.
  209. *
  210. * @return A token (SDWebImageDownloadToken) that can be passed to -cancel: to cancel this operation
  211. */
  212. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  213. options:(SDWebImageDownloaderOptions)options
  214. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  215. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  216. /**
  217. * Cancels a download that was previously queued using -downloadImageWithURL:options:progress:completed:
  218. *
  219. * @param token The token received from -downloadImageWithURL:options:progress:completed: that should be canceled.
  220. */
  221. - (void)cancel:(nullable SDWebImageDownloadToken *)token;
  222. /**
  223. * Sets the download queue suspension state
  224. */
  225. - (void)setSuspended:(BOOL)suspended;
  226. /**
  227. * Cancels all download operations in the queue
  228. */
  229. - (void)cancelAllDownloads;
  230. /**
  231. * Forces SDWebImageDownloader to create and use a new NSURLSession that is
  232. * initialized with the given configuration.
  233. * @note All existing download operations in the queue will be cancelled.
  234. * @note `timeoutIntervalForRequest` is going to be overwritten.
  235. *
  236. * @param sessionConfiguration The configuration to use for the new NSURLSession
  237. */
  238. - (void)createNewSessionWithConfiguration:(nonnull NSURLSessionConfiguration *)sessionConfiguration;
  239. /**
  240. * Invalidates the managed session, optionally canceling pending operations.
  241. * @note If you use custom downloader instead of the shared downloader, you need call this method when you do not use it to avoid memory leak
  242. * @param cancelPendingOperations Whether or not to cancel pending operations.
  243. * @note Calling this method on the shared downloader has no effect.
  244. */
  245. - (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations;
  246. @end