SDWebImageDownloader.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * gif download
  55. */
  56. SDWebImageDownloaderGifIncluded = 1 << 9
  57. };
  58. typedef NS_ENUM(NSInteger, SDWebImageDownloaderExecutionOrder) {
  59. /**
  60. * Default value. All download operations will execute in queue style (first-in-first-out).
  61. */
  62. SDWebImageDownloaderFIFOExecutionOrder,
  63. /**
  64. * All download operations will execute in stack style (last-in-first-out).
  65. */
  66. SDWebImageDownloaderLIFOExecutionOrder
  67. };
  68. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStartNotification;
  69. FOUNDATION_EXPORT NSString * _Nonnull const SDWebImageDownloadStopNotification;
  70. typedef void(^SDWebImageDownloaderProgressBlock)(NSInteger receivedSize, NSInteger expectedSize, NSURL * _Nullable targetURL);
  71. typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BOOL finished);
  72. typedef NSDictionary<NSString *, NSString *> SDHTTPHeadersDictionary;
  73. typedef NSMutableDictionary<NSString *, NSString *> SDHTTPHeadersMutableDictionary;
  74. typedef SDHTTPHeadersDictionary * _Nullable (^SDWebImageDownloaderHeadersFilterBlock)(NSURL * _Nullable url, SDHTTPHeadersDictionary * _Nullable headers);
  75. /**
  76. * A token associated with each download. Can be used to cancel a download
  77. */
  78. @interface SDWebImageDownloadToken : NSObject <SDWebImageOperation>
  79. /**
  80. The download's URL. This should be readonly and you should not modify
  81. */
  82. @property (nonatomic, strong, nullable) NSURL *url;
  83. /**
  84. The cancel token taken from `addHandlersForProgress:completed`. This should be readonly and you should not modify
  85. @note use `-[SDWebImageDownloadToken cancel]` to cancel the token
  86. */
  87. @property (nonatomic, strong, nullable) id downloadOperationCancelToken;
  88. @end
  89. /**
  90. * Asynchronous downloader dedicated and optimized for image loading.
  91. */
  92. @interface SDWebImageDownloader : NSObject
  93. /**
  94. * Decompressing images that are downloaded and cached can improve performance but can consume lot of memory.
  95. * Defaults to YES. Set this to NO if you are experiencing a crash due to excessive memory consumption.
  96. */
  97. @property (assign, nonatomic) BOOL shouldDecompressImages;
  98. /**
  99. * The maximum number of concurrent downloads
  100. */
  101. @property (assign, nonatomic) NSInteger maxConcurrentDownloads;
  102. /**
  103. * Shows the current amount of downloads that still need to be downloaded
  104. */
  105. @property (readonly, nonatomic) NSUInteger currentDownloadCount;
  106. /**
  107. * The timeout value (in seconds) for the download operation. Default: 15.0.
  108. */
  109. @property (assign, nonatomic) NSTimeInterval downloadTimeout;
  110. /**
  111. * The configuration in use by the internal NSURLSession.
  112. * Mutating this object directly has no effect.
  113. *
  114. * @see createNewSessionWithConfiguration:
  115. */
  116. @property (readonly, nonatomic, nonnull) NSURLSessionConfiguration *sessionConfiguration;
  117. /**
  118. * Changes download operations execution order. Default value is `SDWebImageDownloaderFIFOExecutionOrder`.
  119. */
  120. @property (assign, nonatomic) SDWebImageDownloaderExecutionOrder executionOrder;
  121. /**
  122. * Singleton method, returns the shared instance
  123. *
  124. * @return global shared instance of downloader class
  125. */
  126. + (nonnull instancetype)sharedDownloader;
  127. /**
  128. * Set the default URL credential to be set for request operations.
  129. */
  130. @property (strong, nonatomic, nullable) NSURLCredential *urlCredential;
  131. /**
  132. * Set username
  133. */
  134. @property (strong, nonatomic, nullable) NSString *username;
  135. /**
  136. * Set password
  137. */
  138. @property (strong, nonatomic, nullable) NSString *password;
  139. /**
  140. * Set filter to pick headers for downloading image HTTP request.
  141. *
  142. * This block will be invoked for each downloading image request, returned
  143. * NSDictionary will be used as headers in corresponding HTTP request.
  144. */
  145. @property (nonatomic, copy, nullable) SDWebImageDownloaderHeadersFilterBlock headersFilter;
  146. /**
  147. * Creates an instance of a downloader with specified session configuration.
  148. * @note `timeoutIntervalForRequest` is going to be overwritten.
  149. * @return new instance of downloader class
  150. */
  151. - (nonnull instancetype)initWithSessionConfiguration:(nullable NSURLSessionConfiguration *)sessionConfiguration NS_DESIGNATED_INITIALIZER;
  152. /**
  153. * Set a value for a HTTP header to be appended to each download HTTP request.
  154. *
  155. * @param value The value for the header field. Use `nil` value to remove the header.
  156. * @param field The name of the header field to set.
  157. */
  158. - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field;
  159. /**
  160. * Returns the value of the specified HTTP header field.
  161. *
  162. * @return The value associated with the header field field, or `nil` if there is no corresponding header field.
  163. */
  164. - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field;
  165. /**
  166. * Sets a subclass of `NSOperation` and conforms to `SDWebImageDownloaderOperationInterface`.
  167. * Default is `SDWebImageDownloaderOperation`.
  168. * Can be used each time SDWebImage constructs a request
  169. * operation to download an image.
  170. *
  171. * @param operationClass The subclass of `NSOperation` and conforms to `SDWebImageDownloaderOperationInterface`.
  172. * Default is `SDWebImageDownloaderOperation`, Passing `nil` will revert to `SDWebImageDownloaderOperation`.
  173. */
  174. - (void)setOperationClass:(nullable Class)operationClass;
  175. /**
  176. * Creates a SDWebImageDownloader async downloader instance with a given URL
  177. *
  178. * The delegate will be informed when the image is finish downloaded or an error has happen.
  179. *
  180. * @see SDWebImageDownloaderDelegate
  181. *
  182. * @param url The URL to the image to download
  183. * @param options The options to be used for this download
  184. * @param progressBlock A block called repeatedly while the image is downloading
  185. * @note the progress block is executed on a background queue
  186. * @param completedBlock A block called once the download is completed.
  187. * If the download succeeded, the image parameter is set, in case of error,
  188. * error parameter is set with the error. The last parameter is always YES
  189. * if SDWebImageDownloaderProgressiveDownload isn't use. With the
  190. * SDWebImageDownloaderProgressiveDownload option, this block is called
  191. * repeatedly with the partial image object and the finished argument set to NO
  192. * before to be called a last time with the full image and finished argument
  193. * set to YES. In case of error, the finished argument is always YES.
  194. *
  195. * @return A token (SDWebImageDownloadToken) that can be passed to -cancel: to cancel this operation
  196. */
  197. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  198. options:(SDWebImageDownloaderOptions)options
  199. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  200. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  201. /**
  202. * Cancels a download that was previously queued using -downloadImageWithURL:options:progress:completed:
  203. *
  204. * @param token The token received from -downloadImageWithURL:options:progress:completed: that should be canceled.
  205. */
  206. - (void)cancel:(nullable SDWebImageDownloadToken *)token;
  207. /**
  208. * Sets the download queue suspension state
  209. */
  210. - (void)setSuspended:(BOOL)suspended;
  211. /**
  212. * Cancels all download operations in the queue
  213. */
  214. - (void)cancelAllDownloads;
  215. /**
  216. * Forces SDWebImageDownloader to create and use a new NSURLSession that is
  217. * initialized with the given configuration.
  218. * @note All existing download operations in the queue will be cancelled.
  219. * @note `timeoutIntervalForRequest` is going to be overwritten.
  220. *
  221. * @param sessionConfiguration The configuration to use for the new NSURLSession
  222. */
  223. - (void)createNewSessionWithConfiguration:(nonnull NSURLSessionConfiguration *)sessionConfiguration;
  224. /**
  225. * Invalidates the managed session, optionally canceling pending operations.
  226. * @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
  227. * @param cancelPendingOperations Whether or not to cancel pending operations.
  228. * @note Calling this method on the shared downloader has no effect.
  229. */
  230. - (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations;
  231. @end