BU_SDWebImageDownloader.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 "BU_SDWebImageCompat.h"
  10. #import "BU_SDWebImageDefine.h"
  11. #import "BU_SDWebImageOperation.h"
  12. #import "BU_SDWebImageDownloaderConfig.h"
  13. #import "BU_SDWebImageDownloaderRequestModifier.h"
  14. #import "BU_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 `BU_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. * By default, when you use `BU_SDWebImageContextAnimatedImageClass` context option (like using `SDAnimatedImageView` which designed to use `BU_SDAnimatedImage`), we may still use `UIImage` when the memory cache hit, or image decoder is not available, to behave as a fallback solution.
  76. * Using this option, can ensure we always produce image with your provided class. If failed, a error with code `SDWebImageErrorBadImageData` will been used.
  77. * Note this options is not compatible with `SDWebImageDownloaderDecodeFirstFrameOnly`, which always produce a UIImage/NSImage.
  78. */
  79. SDWebImageDownloaderMatchAnimatedImageClass = 1 << 12,
  80. };
  81. FOUNDATION_EXPORT NSNotificationName _Nonnull const BU_SDWebImageDownloadStartNotification;
  82. FOUNDATION_EXPORT NSNotificationName _Nonnull const BU_SDWebImageDownloadReceiveResponseNotification;
  83. FOUNDATION_EXPORT NSNotificationName _Nonnull const BU_SDWebImageDownloadStopNotification;
  84. FOUNDATION_EXPORT NSNotificationName _Nonnull const BU_SDWebImageDownloadFinishNotification;
  85. typedef SDImageLoaderProgressBlock SDWebImageDownloaderProgressBlock;
  86. typedef SDImageLoaderCompletedBlock SDWebImageDownloaderCompletedBlock;
  87. /**
  88. * A token associated with each download. Can be used to cancel a download
  89. */
  90. @interface BU_SDWebImageDownloadToken : NSObject <BU_SDWebImageOperation>
  91. /**
  92. Cancel the current download.
  93. */
  94. - (void)cancel;
  95. /**
  96. The download's URL.
  97. */
  98. @property (nonatomic, strong, nullable, readonly) NSURL *url;
  99. /**
  100. The download's request.
  101. */
  102. @property (nonatomic, strong, nullable, readonly) NSURLRequest *request;
  103. /**
  104. The download's response.
  105. */
  106. @property (nonatomic, strong, nullable, readonly) NSURLResponse *response;
  107. @end
  108. /**
  109. * Asynchronous downloader dedicated and optimized for image loading.
  110. */
  111. @interface BU_SDWebImageDownloader : NSObject
  112. /**
  113. * Downloader Config object - storing all kind of settings.
  114. * Most config properties support dynamic changes during download, except something like `sessionConfiguration`, see `BU_SDWebImageDownloaderConfig` for more detail.
  115. */
  116. @property (nonatomic, copy, readonly, nonnull) BU_SDWebImageDownloaderConfig *config;
  117. /**
  118. * Set the request modifier to modify the original download request before image load.
  119. * 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.
  120. * Defaults to nil, means does not modify the original download request.
  121. * @note If you want to modify single request, consider using `BU_SDWebImageContextDownloadRequestModifier` context option.
  122. */
  123. @property (nonatomic, strong, nullable) id<SDWebImageDownloaderRequestModifier> requestModifier;
  124. /**
  125. * The configuration in use by the internal NSURLSession. If you want to provide a custom sessionConfiguration, use `BU_SDWebImageDownloaderConfig.sessionConfiguration` and create a new downloader instance.
  126. @note This is immutable according to NSURLSession's documentation. Mutating this object directly has no effect.
  127. */
  128. @property (nonatomic, readonly, nonnull) NSURLSessionConfiguration *sessionConfiguration;
  129. /**
  130. * Gets/Sets the download queue suspension state.
  131. */
  132. @property (nonatomic, assign, getter=isSuspended) BOOL suspended;
  133. /**
  134. * Shows the current amount of downloads that still need to be downloaded
  135. */
  136. @property (nonatomic, assign, readonly) NSUInteger currentDownloadCount;
  137. /**
  138. * Returns the global shared downloader instance. Which use the `BU_SDWebImageDownloaderConfig.defaultDownloaderConfig` config.
  139. */
  140. @property (nonatomic, class, readonly, nonnull) BU_SDWebImageDownloader *sharedDownloader;
  141. /**
  142. Creates an instance of a downloader with specified downloader config.
  143. You can specify session configuration, timeout or operation class through downloader config.
  144. @param config The downloader config. If you specify nil, the `defaultDownloaderConfig` will be used.
  145. @return new instance of downloader class
  146. */
  147. - (nonnull instancetype)initWithConfig:(nullable BU_SDWebImageDownloaderConfig *)config 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 field.
  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. * Creates a SDWebImageDownloader async downloader instance with a given URL
  163. *
  164. * The delegate will be informed when the image is finish downloaded or an error has happen.
  165. *
  166. * @see SDWebImageDownloaderDelegate
  167. *
  168. * @param url The URL to the image to download
  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 used to cancel this operation
  179. */
  180. //- (nullable BU_SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  181. // completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  182. /**
  183. * Creates a SDWebImageDownloader async downloader instance with a given URL
  184. *
  185. * The delegate will be informed when the image is finish downloaded or an error has happen.
  186. *
  187. * @see SDWebImageDownloaderDelegate
  188. *
  189. * @param url The URL to the image to download
  190. * @param options The options to be used for this download
  191. * @param progressBlock A block called repeatedly while the image is downloading
  192. * @note the progress block is executed on a background queue
  193. * @param completedBlock A block called once the download is completed.
  194. * If the download succeeded, the image parameter is set, in case of error,
  195. * error parameter is set with the error. The last parameter is always YES
  196. * if SDWebImageDownloaderProgressiveLoad isn't use. With the
  197. * SDWebImageDownloaderProgressiveLoad option, this block is called
  198. * repeatedly with the partial image object and the finished argument set to NO
  199. * before to be called a last time with the full image and finished argument
  200. * set to YES. In case of error, the finished argument is always YES.
  201. *
  202. * @return A token (SDWebImageDownloadToken) that can be used to cancel this operation
  203. */
  204. - (nullable BU_SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  205. options:(SDWebImageDownloaderOptions)options
  206. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  207. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  208. /**
  209. * Creates a SDWebImageDownloader async downloader instance with a given URL
  210. *
  211. * The delegate will be informed when the image is finish downloaded or an error has happen.
  212. *
  213. * @see SDWebImageDownloaderDelegate
  214. *
  215. * @param url The URL to the image to download
  216. * @param options The options to be used for this download
  217. * @param context A context contains different options to perform specify changes or processes, see `BU_SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  218. * @param progressBlock A block called repeatedly while the image is downloading
  219. * @note the progress block is executed on a background queue
  220. * @param completedBlock A block called once the download is completed.
  221. *
  222. * @return A token (SDWebImageDownloadToken) that can be used to cancel this operation
  223. */
  224. - (nullable BU_SDWebImageDownloadToken *)downloadImageWithURL:(nullable NSURL *)url
  225. options:(SDWebImageDownloaderOptions)options
  226. context:(nullable SDWebImageContext *)context
  227. progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock
  228. completed:(nullable SDWebImageDownloaderCompletedBlock)completedBlock;
  229. /**
  230. * Cancels all download operations in the queue
  231. */
  232. //- (void)cancelAllDownloads;
  233. /**
  234. * Invalidates the managed session, optionally canceling pending operations.
  235. * @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
  236. * @param cancelPendingOperations Whether or not to cancel pending operations.
  237. * @note Calling this method on the shared downloader has no effect.
  238. */
  239. //- (void)invalidateSessionAndCancel:(BOOL)cancelPendingOperations;
  240. @end
  241. /**
  242. SDWebImageDownloader is the built-in image loader conform to `SDImageLoader`. Which provide the HTTP/HTTPS/FTP download, or local file URL using NSURLSession.
  243. However, this downloader class itself also support customization for advanced users. You can specify `operationClass` in download config to custom download operation, See `SDWebImageDownloaderOperation`.
  244. If you want to provide some image loader which beyond network or local file, consider to create your own custom class conform to `SDImageLoader`.
  245. */
  246. @interface BU_SDWebImageDownloader (SDImageLoader) <BU_SDImageLoader>
  247. @end