SDWebImageDownloader.h 9.1 KB

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