2
0

SDWebImageDownloader.h 9.2 KB

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