SDWebImageDownloader.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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 "SDWebImageDefine.h"
  11. #import "SDWebImageOperation.h"
  12. #import "SDWebImageDownloaderConfig.h"
  13. #import "SDWebImageDownloaderRequestModifier.h"
  14. #import "SDImageLoader.h"
  15. /// Downloader options
  16. typedef NS_OPTIONS(NSUInteger, SDWebImageDownloaderOptions) {
  17. /**
  18. * Put the download in the low queue priority and task priority.
  19. */
  20. SDWebImageDownloaderLowPriority = 1 << 0,
  21. /**
  22. * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
  23. */
  24. SDWebImageDownloaderProgressiveLoad = 1 << 1,
  25. /**
  26. * By default, request prevent the use of NSURLCache. With this flag, NSURLCache
  27. * is used with default policies.
  28. */
  29. SDWebImageDownloaderUseNSURLCache = 1 << 2,
  30. /**
  31. * Call completion block with nil image/imageData if the image was read from NSURLCache
  32. * And the error code is `SDWebImageErrorCacheNotModified`
  33. * This flag should be combined with `SDWebImageDownloaderUseNSURLCache`.
  34. */
  35. SDWebImageDownloaderIgnoreCachedResponse = 1 << 3,
  36. /**
  37. * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
  38. * extra time in background to let the request finish. If the background task expires the operation will be cancelled.
  39. */
  40. SDWebImageDownloaderContinueInBackground = 1 << 4,
  41. /**
  42. * Handles cookies stored in NSHTTPCookieStore by setting
  43. * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
  44. */
  45. SDWebImageDownloaderHandleCookies = 1 << 5,
  46. /**
  47. * Enable to allow untrusted SSL certificates.
  48. * Useful for testing purposes. Use with caution in production.
  49. */
  50. SDWebImageDownloaderAllowInvalidSSLCertificates = 1 << 6,
  51. /**
  52. * Put the download in the high queue priority and task priority.
  53. */
  54. SDWebImageDownloaderHighPriority = 1 << 7,
  55. /**
  56. * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
  57. * images to a size compatible with the constrained memory of devices.
  58. * This flag take no effect if `SDWebImageDownloaderAvoidDecodeImage` is set. And it will be ignored if `SDWebImageDownloaderProgressiveLoad` is set.
  59. */
  60. SDWebImageDownloaderScaleDownLargeImages = 1 << 8,
  61. /**
  62. * By default, we will decode the image in the background during cache query and download from the network. This can help to improve performance because when rendering image on the screen, it need to be firstly decoded. But this happen on the main queue by Core Animation.
  63. * However, this process may increase the memory usage as well. If you are experiencing a issue due to excessive memory consumption, This flag can prevent decode the image.
  64. */
  65. SDWebImageDownloaderAvoidDecodeImage = 1 << 9,
  66. /**
  67. * By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
  68. */
  69. SDWebImageDownloaderDecodeFirstFrameOnly = 1 << 10,
  70. /**
  71. * By default, for `SDAnimatedImage`, we decode the animated image frame during rendering to reduce memory usage. This flag actually trigger `preloadAllAnimatedImageFrames = YES` after image load from network
  72. */
  73. SDWebImageDownloaderPreloadAllFrames = 1 << 11
  74. };
  75. FOUNDATION_EXPORT NSNotificationName _Nonnull const SDWebImageDownloadStartNotification;
  76. FOUNDATION_EXPORT NSNotificationName _Nonnull const SDWebImageDownloadReceiveResponseNotification;
  77. FOUNDATION_EXPORT NSNotificationName _Nonnull const SDWebImageDownloadStopNotification;
  78. FOUNDATION_EXPORT NSNotificationName _Nonnull const SDWebImageDownloadFinishNotification;
  79. typedef SDImageLoaderProgressBlock SDWebImageDownloaderProgressBlock;
  80. typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
  81. /**
  82. * A token associated with each download. Can be used to cancel a download
  83. */
  84. @interface SDWebImageDownloadToken : NSObject <SDWebImageOperation>
  85. /**
  86. Cancel the current download.
  87. */
  88. - (void)cancel;
  89. /**
  90. The download's URL.
  91. */
  92. @property (nonatomic, strong, nullable, readonly) NSURL *url;
  93. /**
  94. The download's request.
  95. */
  96. @property (nonatomic, strong, nullable, readonly) NSURLRequest *request;
  97. /**
  98. The download's response.
  99. */
  100. @property (nonatomic, strong, nullable, readonly) NSURLResponse *response;
  101. @end
  102. /**
  103. * Asynchronous downloader dedicated and optimized for image loading.
  104. */
  105. @interface SDWebImageDownloader : NSObject
  106. /**
  107. * Downloader Config object - storing all kind of settings.
  108. * Most config properties support dynamic changes during download, except something like `sessionConfiguration`, see `SDWebImageDownloaderConfig` for more detail.
  109. */
  110. @property (nonatomic, copy, readonly, nonnull) SDWebImageDownloaderConfig *config;
  111. /**
  112. * Set the request modifier to modify the original download request before image load.
  113. * This request modifier method will be called for each downloading image request. Return the original request means no modication. Return nil will cancel the download request.
  114. * Defaults to nil, means does not modify the original download request.
  115. * @note If you want to modify single request, consider using `SDWebImageContextDownloadRequestModifier` context option.
  116. */
  117. @property (nonatomic, strong, nullable) id<SDWebImageDownloaderRequestModifier> requestModifier;
  118. /**
  119. * The configuration in use by the internal NSURLSession. If you want to provide a custom sessionConfiguration, use `SDWebImageDownloaderConfig.sessionConfiguration` and create a new downloader instance.
  120. @note This is immutable according to NSURLSession's documentation. Mutating this object directly has no effect.
  121. */
  122. @property (nonatomic, readonly, nonnull) NSURLSessionConfiguration *sessionConfiguration;
  123. /**
  124. * Gets/Sets the download queue suspension state.
  125. */
  126. @property (nonatomic, assign, getter=isSuspended) BOOL suspended;
  127. /**
  128. * Shows the current amount of downloads that still need to be downloaded
  129. */
  130. @property (nonatomic, assign, readonly) NSUInteger currentDownloadCount;
  131. /**
  132. * Returns the global shared downloader instance. Which use the `SDWebImageDownloaderConfig.defaultDownloaderConfig` config.
  133. */
  134. @property (nonatomic, class, readonly, nonnull) SDWebImageDownloader *sharedDownloader;
  135. /**
  136. Creates an instance of a downloader with specified downloader config.
  137. You can specify session configuration, timeout or operation class through downloader config.
  138. @param config The downloader config. If you specify nil, the `defaultDownloaderConfig` will be used.
  139. @return new instance of downloader class
  140. */
  141. - (nonnull instancetype)initWithConfig:(nullable SDWebImageDownloaderConfig *)config NS_DESIGNATED_INITIALIZER;
  142. /**
  143. * Set a value for a HTTP header to be appended to each download HTTP request.
  144. *
  145. * @param value The value for the header field. Use `nil` value to remove the header field.
  146. * @param field The name of the header field to set.
  147. */
  148. - (void)setValue:(nullable NSString *)value forHTTPHeaderField:(nullable NSString *)field;
  149. /**
  150. * Returns the value of the specified HTTP header field.
  151. *
  152. * @return The value associated with the header field field, or `nil` if there is no corresponding header field.
  153. */
  154. - (nullable NSString *)valueForHTTPHeaderField:(nullable NSString *)field;
  155. /**
  156. * Creates a SDWebImageDownloader async downloader instance with a given URL
  157. *
  158. * The delegate will be informed when the image is finish downloaded or an error has happen.
  159. *
  160. * @see SDWebImageDownloaderDelegate
  161. *
  162. * @param url The URL to the image to download
  163. * @param completedBlock A block called once the download is completed.
  164. * If the download succeeded, the image parameter is set, in case of error,
  165. * error parameter is set with the error. The last parameter is always YES
  166. * if SDWebImageDownloaderProgressiveDownload isn't use. With the
  167. * SDWebImageDownloaderProgressiveDownload option, this block is called
  168. * repeatedly with the partial image object and the finished argument set to NO
  169. * before to be called a last time with the full image and finished argument
  170. * set to YES. In case of error, the finished argument is always YES.
  171. *
  172. * @return A token (SDWebImageDownloadToken) that can be used to cancel this operation
  173. */
  174. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  175. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  176. /**
  177. * Creates a SDWebImageDownloader async downloader instance with a given URL
  178. *
  179. * The delegate will be informed when the image is finish downloaded or an error has happen.
  180. *
  181. * @see SDWebImageDownloaderDelegate
  182. *
  183. * @param url The URL to the image to download
  184. * @param options The options to be used for this download
  185. * @param progressBlock A block called repeatedly while the image is downloading
  186. * @note the progress block is executed on a background queue
  187. * @param completedBlock A block called once the download is completed.
  188. * If the download succeeded, the image parameter is set, in case of error,
  189. * error parameter is set with the error. The last parameter is always YES
  190. * if SDWebImageDownloaderProgressiveLoad isn't use. With the
  191. * SDWebImageDownloaderProgressiveLoad option, this block is called
  192. * repeatedly with the partial image object and the finished argument set to NO
  193. * before to be called a last time with the full image and finished argument
  194. * set to YES. In case of error, the finished argument is always YES.
  195. *
  196. * @return A token (SDWebImageDownloadToken) that can be used to cancel this operation
  197. */
  198. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  199. options:(SDWebImageDownloaderOptions)options
  200. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  201. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  202. /**
  203. * Creates a SDWebImageDownloader async downloader instance with a given URL
  204. *
  205. * The delegate will be informed when the image is finish downloaded or an error has happen.
  206. *
  207. * @see SDWebImageDownloaderDelegate
  208. *
  209. * @param url The URL to the image to download
  210. * @param options The options to be used for this download
  211. * @param context A context contains different options to perform specify changes or processes, see `SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  212. * @param progressBlock A block called repeatedly while the image is downloading
  213. * @note the progress block is executed on a background queue
  214. * @param completedBlock A block called once the download is completed.
  215. *
  216. * @return A token (SDWebImageDownloadToken) that can be used to cancel this operation
  217. */
  218. - (nullable SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  219. options:(SDWebImageDownloaderOptions)options
  220. context:(nullable SDWebImageContext *)context
  221. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  222. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  223. /**
  224. * Cancels all download operations in the queue
  225. */
  226. - (void)cancelAllDownloads;
  227. /**
  228. * Invalidates the managed session, optionally canceling pending operations.
  229. * @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
  230. * @param cancelPendingOperations Whether or not to cancel pending operations.
  231. * @note Calling this method on the shared downloader has no effect.
  232. */
  233. - (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations;
  234. @end
  235. /**
  236. SDWebImageDownloader is the built-in image loader conform to `SDImageLoader`. Which provide the HTTP/HTTPS/FTP download, or local file URL using NSURLSession.
  237. However, this downloader class itself also support customization for advanced users. You can specify `operationClass` in download config to custom download operation, See `SDWebImageDownloaderOperation`.
  238. If you want to provide some image loader which beyond network or local file, consider to create your own custom class conform to `SDImageLoader`.
  239. */
  240. @interface SDWebImageDownloader (SDImageLoader) <SDImageLoader>
  241. @end