BU_SDWebImageManager.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 "BU_SDWebImageCompat.h"
  9. #import "BU_SDWebImageOperation.h"
  10. #import "BU_SDImageCacheDefine.h"
  11. #import "BU_SDImageLoader.h"
  12. #import "BU_SDImageTransformer.h"
  13. #import "BU_SDWebImageCacheKeyFilter.h"
  14. #import "BU_SDWebImageCacheSerializer.h"
  15. #import "BU_SDWebImageOptionsProcessor.h"
  16. typedef void(^BU_SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, BU_SDImageCacheType cacheType, NSURL * _Nullable imageURL);
  17. typedef void(^BU_SDInternalCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, BU_SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL);
  18. /**
  19. A combined operation representing the cache and loader operation. You can use it to cancel the load process.
  20. */
  21. @interface BU_SDWebImageCombinedOperation : NSObject <BU_SDWebImageOperation>
  22. /**
  23. Cancel the current operation, including cache and loader process
  24. */
  25. - (void)cancel;
  26. /**
  27. The cache operation from the image cache query
  28. */
  29. @property (strong, nonatomic, nullable, readonly) id<BU_SDWebImageOperation> cacheOperation;
  30. /**
  31. The loader operation from the image loader (such as download operation)
  32. */
  33. @property (strong, nonatomic, nullable, readonly) id<BU_SDWebImageOperation> loaderOperation;
  34. @end
  35. @class BU_SDWebImageManager;
  36. /**
  37. The manager delegate protocol.
  38. */
  39. @protocol BU_SDWebImageManagerDelegate <NSObject>
  40. @optional
  41. /**
  42. * Controls which image should be downloaded when the image is not found in the cache.
  43. *
  44. * @param imageManager The current `SDWebImageManager`
  45. * @param imageURL The url of the image to be downloaded
  46. *
  47. * @return Return NO to prevent the downloading of the image on cache misses. If not implemented, YES is implied.
  48. */
  49. - (BOOL)imageManager:(nonnull BU_SDWebImageManager *)imageManager shouldDownloadImageForURL:(nonnull NSURL *)imageURL;
  50. /**
  51. * Controls the complicated logic to mark as failed URLs when download error occur.
  52. * If the delegate implement this method, we will not use the built-in way to mark URL as failed based on error code;
  53. @param imageManager The current `SDWebImageManager`
  54. @param imageURL The url of the image
  55. @param error The download error for the url
  56. @return Whether to block this url or not. Return YES to mark this URL as failed.
  57. */
  58. - (BOOL)imageManager:(nonnull BU_SDWebImageManager *)imageManager shouldBlockFailedURL:(nonnull NSURL *)imageURL withError:(nonnull NSError *)error;
  59. @end
  60. /**
  61. * The SDWebImageManager is the class behind the UIImageView+BU_WebCache category and likes.
  62. * It ties the asynchronous downloader (SDWebImageDownloader) with the image cache store (SDImageCache).
  63. * You can use this class directly to benefit from web image downloading with caching in another context than
  64. * a UIView.
  65. *
  66. * Here is a simple example of how to use SDWebImageManager:
  67. *
  68. * @code
  69. SDWebImageManager *manager = [SDWebImageManager sharedManager];
  70. [manager loadImageWithURL:imageURL
  71. options:0
  72. progress:nil
  73. completed:^(UIImage *image, NSError *error, BU_SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  74. if (image) {
  75. // do something with image
  76. }
  77. }];
  78. * @endcode
  79. */
  80. @interface BU_SDWebImageManager : NSObject
  81. /**
  82. * The delegate for manager. Defatuls to nil.
  83. */
  84. @property (weak, nonatomic, nullable) id <BU_SDWebImageManagerDelegate> delegate;
  85. /**
  86. * The image cache used by manager to query image cache.
  87. */
  88. @property (strong, nonatomic, readonly, nonnull) id<BU_SDImageCache> imageCache;
  89. /**
  90. * The image loader used by manager to load image.
  91. */
  92. @property (strong, nonatomic, readonly, nonnull) id<BU_SDImageLoader> imageLoader;
  93. /**
  94. The image transformer for manager. It's used for image transform after the image load finished and store the transformed image to cache, see `BU_SDImageTransformer`.
  95. Defaults to nil, which means no transform is applied.
  96. @note This will affect all the load requests for this manager if you provide. However, you can pass `BU_SDWebImageContextImageTransformer` in context arg to explicitly use that transformer instead.
  97. */
  98. @property (strong, nonatomic, nullable) id<BU_SDImageTransformer> transformer;
  99. /**
  100. * The cache filter is used to convert an URL into a cache key each time SDWebImageManager need cache key to use image cache.
  101. *
  102. * The following example sets a filter in the application delegate that will remove any query-string from the
  103. * URL before to use it as a cache key:
  104. *
  105. * @code
  106. SDWebImageManager.sharedManager.cacheKeyFilter =[BU_SDWebImageCacheKeyFilter cacheKeyFilterWithBlock:^NSString * _Nullable(NSURL * _Nonnull url) {
  107. url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
  108. return [url absoluteString];
  109. }];
  110. * @endcode
  111. */
  112. @property (nonatomic, strong, nullable) id<BU_SDWebImageCacheKeyFilter> cacheKeyFilter;
  113. /**
  114. * The cache serializer is used to convert the decoded image, the source downloaded data, to the actual data used for storing to the disk cache. If you return nil, means to generate the data from the image instance, see `SDImageCache`.
  115. * For example, if you are using WebP images and facing the slow decoding time issue when later retriving from disk cache again. You can try to encode the decoded image to JPEG/PNG format to disk cache instead of source downloaded data.
  116. * @note The `image` arg is nonnull, but when you also provide a image transformer and the image is transformed, the `data` arg may be nil, take attention to this case.
  117. * @note This method is called from a global queue in order to not to block the main thread.
  118. * @code
  119. SDWebImageManager.sharedManager.cacheSerializer = [BU_SDWebImageCacheSerializer cacheSerializerWithBlock:^NSData * _Nullable(UIImage * _Nonnull image, NSData * _Nullable data, NSURL * _Nullable imageURL) {
  120. BU_SDImageFormat format = [NSData sdBu_imageFormatForImageData:data];
  121. switch (format) {
  122. case BU_SDImageFormatWebP:
  123. return image.images ? data : nil;
  124. default:
  125. return data;
  126. }
  127. }];
  128. * @endcode
  129. * The default value is nil. Means we just store the source downloaded data to disk cache.
  130. */
  131. @property (nonatomic, strong, nullable) id<BU_SDWebImageCacheSerializer> cacheSerializer;
  132. /**
  133. The options processor is used, to have a global control for all the image request options and context option for current manager.
  134. @note If you use `transformer`, `cacheKeyFilter` or `cacheSerializer` property of manager, the input context option already apply those properties before passed. This options processor is a better replacement for those property in common usage.
  135. For example, you can control the global options, based on the URL or original context option like the below code.
  136. @code
  137. SDWebImageManager.sharedManager.optionsProcessor = [BU_SDWebImageOptionsProcessor optionsProcessorWithBlock:^BU_SDWebImageOptionsResult * _Nullable(NSURL * _Nullable url, BU_SDWebImageOptions options, SDWebImageContext * _Nullable context) {
  138. // Only do animation on `SDAnimatedImageView`
  139. if (!context[BU_SDWebImageContextAnimatedImageClass]) {
  140. options |= SDWebImageDecodeFirstFrameOnly;
  141. }
  142. // Do not force decode for png url
  143. if ([url.lastPathComponent isEqualToString:@"png"]) {
  144. options |= SDWebImageAvoidDecodeImage;
  145. }
  146. // Always use screen scale factor
  147. SDWebImageMutableContext *mutableContext = [NSDictionary dictionaryWithDictionary:context];
  148. mutableContext[BU_SDWebImageContextImageScaleFactor] = @(UIScreen.mainScreen.scale);
  149. context = [mutableContext copy];
  150. return [[BU_SDWebImageOptionsResult alloc] initWithOptions:options context:context];
  151. }];
  152. @endcode
  153. */
  154. @property (nonatomic, strong, nullable) id<BU_SDWebImageOptionsProcessor> optionsProcessor;
  155. /**
  156. * Check one or more operations running
  157. */
  158. @property (nonatomic, assign, readonly, getter=isRunning) BOOL running;
  159. /**
  160. The default image cache when the manager which is created with no arguments. Such as shared manager or init.
  161. Defaults to nil. Means using `SDImageCache.sharedImageCache`
  162. */
  163. @property (nonatomic, class, nullable) id<BU_SDImageCache> defaultImageCache;
  164. /**
  165. The default image loader for manager which is created with no arguments. Such as shared manager or init.
  166. Defaults to nil. Means using `SDWebImageDownloader.sharedDownloader`
  167. */
  168. @property (nonatomic, class, nullable) id<BU_SDImageLoader> defaultImageLoader;
  169. /**
  170. * Returns global shared manager instance.
  171. */
  172. @property (nonatomic, class, readonly, nonnull) BU_SDWebImageManager *sharedManager;
  173. /**
  174. * Allows to specify instance of cache and image loader used with image manager.
  175. * @return new instance of `SDWebImageManager` with specified cache and loader.
  176. */
  177. - (nonnull instancetype)initWithCache:(nonnull id<BU_SDImageCache>)cache loader:(nonnull id<BU_SDImageLoader>)loader NS_DESIGNATED_INITIALIZER;
  178. /**
  179. * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  180. *
  181. * @param url The URL to the image
  182. * @param options A mask to specify options to use for this request
  183. * @param progressBlock A block called while image is downloading
  184. * @note the progress block is executed on a background queue
  185. * @param completedBlock A block called when operation has been completed.
  186. *
  187. * This parameter is required.
  188. *
  189. * This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter.
  190. * In case of error the image parameter is nil and the third parameter may contain an NSError.
  191. *
  192. * The forth parameter is an `BU_SDImageCacheType` enum indicating if the image was retrieved from the local cache
  193. * or from the memory cache or from the network.
  194. *
  195. * The fith parameter is set to NO when the SDWebImageProgressiveLoad option is used and the image is
  196. * downloading. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
  197. * block is called a last time with the full image and the last parameter set to YES.
  198. *
  199. * The last parameter is the original image URL
  200. *
  201. * @return Returns an instance of BU_SDWebImageCombinedOperation, which you can cancel the loading process.
  202. */
  203. - (nullable BU_SDWebImageCombinedOperation *)loadImageWithURL:(nullable NSURL *)url
  204. options:(BU_SDWebImageOptions)options
  205. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  206. completed:(nonnull BU_SDInternalCompletionBlock)completedBlock;
  207. /**
  208. * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  209. *
  210. * @param url The URL to the image
  211. * @param options A mask to specify options to use for this request
  212. * @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.
  213. * @param progressBlock A block called while image is downloading
  214. * @note the progress block is executed on a background queue
  215. * @param completedBlock A block called when operation has been completed.
  216. *
  217. * @return Returns an instance of BU_SDWebImageCombinedOperation, which you can cancel the loading process.
  218. */
  219. - (nullable BU_SDWebImageCombinedOperation *)loadImageWithURL:(nullable NSURL *)url
  220. options:(BU_SDWebImageOptions)options
  221. context:(nullable SDWebImageContext *)context
  222. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  223. completed:(nonnull BU_SDInternalCompletionBlock)completedBlock;
  224. /**
  225. * Cancel all current operations
  226. */
  227. //- (void)cancelAll;
  228. /**
  229. * Return the cache key for a given URL
  230. */
  231. - (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url;
  232. @end