SDWebImageManager.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "SDWebImageCompat.h"
  9. #import "SDWebImageOperation.h"
  10. #import "SDImageCacheDefine.h"
  11. #import "SDImageLoader.h"
  12. #import "SDImageTransformer.h"
  13. #import "SDWebImageCacheKeyFilter.h"
  14. #import "SDWebImageCacheSerializer.h"
  15. typedef void(^SDExternalCompletionBlock)(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL);
  16. typedef void(^SDInternalCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, NSError * _Nullable error, SDImageCacheType cacheType, BOOL finished, NSURL * _Nullable imageURL);
  17. /**
  18. A combined operation representing the cache and loader operation. You can use it to cancel the load process.
  19. */
  20. @interface SDWebImageCombinedOperation : NSObject <SDWebImageOperation>
  21. /**
  22. Cancel the current operation, including cache and loader process
  23. */
  24. - (void)cancel;
  25. /**
  26. The cache operation from the image cache query
  27. */
  28. @property (strong, nonatomic, nullable, readonly) id<SDWebImageOperation> cacheOperation;
  29. /**
  30. The loader operation from the image loader (such as download operation)
  31. */
  32. @property (strong, nonatomic, nullable, readonly) id<SDWebImageOperation> loaderOperation;
  33. @end
  34. @class SDWebImageManager;
  35. /**
  36. The manager delegate protocol.
  37. */
  38. @protocol SDWebImageManagerDelegate <NSObject>
  39. @optional
  40. /**
  41. * Controls which image should be downloaded when the image is not found in the cache.
  42. *
  43. * @param imageManager The current `SDWebImageManager`
  44. * @param imageURL The url of the image to be downloaded
  45. *
  46. * @return Return NO to prevent the downloading of the image on cache misses. If not implemented, YES is implied.
  47. */
  48. - (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldDownloadImageForURL:(nonnull NSURL *)imageURL;
  49. /**
  50. * Controls the complicated logic to mark as failed URLs when download error occur.
  51. * If the delegate implement this method, we will not use the built-in way to mark URL as failed based on error code;
  52. @param imageManager The current `SDWebImageManager`
  53. @param imageURL The url of the image
  54. @param error The download error for the url
  55. @return Whether to block this url or not. Return YES to mark this URL as failed.
  56. */
  57. - (BOOL)imageManager:(nonnull SDWebImageManager *)imageManager shouldBlockFailedURL:(nonnull NSURL *)imageURL withError:(nonnull NSError *)error;
  58. @end
  59. /**
  60. * The SDWebImageManager is the class behind the UIImageView+WebCache category and likes.
  61. * It ties the asynchronous downloader (SDWebImageDownloader) with the image cache store (SDImageCache).
  62. * You can use this class directly to benefit from web image downloading with caching in another context than
  63. * a UIView.
  64. *
  65. * Here is a simple example of how to use SDWebImageManager:
  66. *
  67. * @code
  68. SDWebImageManager *manager = [SDWebImageManager sharedManager];
  69. [manager loadImageWithURL:imageURL
  70. options:0
  71. progress:nil
  72. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  73. if (image) {
  74. // do something with image
  75. }
  76. }];
  77. * @endcode
  78. */
  79. @interface SDWebImageManager : NSObject
  80. /**
  81. * The delegate for manager. Defatuls to nil.
  82. */
  83. @property (weak, nonatomic, nullable) id <SDWebImageManagerDelegate> delegate;
  84. /**
  85. * The image cache used by manager to query image cache.
  86. */
  87. @property (strong, nonatomic, readonly, nonnull) id<SDImageCache> imageCache;
  88. /**
  89. * The image loader used by manager to load image.
  90. */
  91. @property (strong, nonatomic, readonly, nonnull) id<SDImageLoader> imageLoader;
  92. /**
  93. The image transformer for manager. It's used for image transform after the image load finished and store the transformed image to cache, see `SDImageTransformer`.
  94. Defaults to nil, which means no transform is applied.
  95. @note This will affect all the load requests for this manager if you provide. However, you can pass `SDWebImageContextImageTransformer` in context arg to explicitly use that transformer instead.
  96. */
  97. @property (strong, nonatomic, nullable) id<SDImageTransformer> transformer;
  98. /**
  99. * The cache filter is used to convert an URL into a cache key each time SDWebImageManager need cache key to use image cache.
  100. *
  101. * The following example sets a filter in the application delegate that will remove any query-string from the
  102. * URL before to use it as a cache key:
  103. *
  104. * @code
  105. SDWebImageManager.sharedManager.cacheKeyFilter =[SDWebImageCacheKeyFilter cacheKeyFilterWithBlock:^NSString * _Nullable(NSURL * _Nonnull url) {
  106. url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
  107. return [url absoluteString];
  108. }];
  109. * @endcode
  110. */
  111. @property (nonatomic, strong, nullable) id<SDWebImageCacheKeyFilter> cacheKeyFilter;
  112. /**
  113. * 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`.
  114. * 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.
  115. * @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.
  116. * @note This method is called from a global queue in order to not to block the main thread.
  117. * @code
  118. SDWebImageManager.sharedManager.cacheSerializer = [SDWebImageCacheSerializer cacheSerializerWithBlock:^NSData * _Nullable(UIImage * _Nonnull image, NSData * _Nullable data, NSURL * _Nullable imageURL) {
  119. SDImageFormat format = [NSData sd_imageFormatForImageData:data];
  120. switch (format) {
  121. case SDImageFormatWebP:
  122. return image.images ? data : nil;
  123. default:
  124. return data;
  125. }
  126. }];
  127. * @endcode
  128. * The default value is nil. Means we just store the source downloaded data to disk cache.
  129. */
  130. @property (nonatomic, strong, nullable) id<SDWebImageCacheSerializer> cacheSerializer;
  131. /**
  132. * Check one or more operations running
  133. */
  134. @property (nonatomic, assign, readonly, getter=isRunning) BOOL running;
  135. /**
  136. The default image cache when the manager which is created with no arguments. Such as shared manager or init.
  137. Defaults to nil. Means using `SDImageCache.sharedImageCache`
  138. */
  139. @property (nonatomic, class, nullable) id<SDImageCache> defaultImageCache;
  140. /**
  141. The default image loader for manager which is created with no arguments. Such as shared manager or init.
  142. Defaults to nil. Means using `SDWebImageDownloader.sharedDownloader`
  143. */
  144. @property (nonatomic, class, nullable) id<SDImageLoader> defaultImageLoader;
  145. /**
  146. * Returns global shared manager instance.
  147. */
  148. @property (nonatomic, class, readonly, nonnull) SDWebImageManager *sharedManager;
  149. /**
  150. * Allows to specify instance of cache and image loader used with image manager.
  151. * @return new instance of `SDWebImageManager` with specified cache and loader.
  152. */
  153. - (nonnull instancetype)initWithCache:(nonnull id<SDImageCache>)cache loader:(nonnull id<SDImageLoader>)loader NS_DESIGNATED_INITIALIZER;
  154. /**
  155. * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  156. *
  157. * @param url The URL to the image
  158. * @param options A mask to specify options to use for this request
  159. * @param progressBlock A block called while image is downloading
  160. * @note the progress block is executed on a background queue
  161. * @param completedBlock A block called when operation has been completed.
  162. *
  163. * This parameter is required.
  164. *
  165. * This block has no return value and takes the requested UIImage as first parameter and the NSData representation as second parameter.
  166. * In case of error the image parameter is nil and the third parameter may contain an NSError.
  167. *
  168. * The forth parameter is an `SDImageCacheType` enum indicating if the image was retrieved from the local cache
  169. * or from the memory cache or from the network.
  170. *
  171. * The fith parameter is set to NO when the SDWebImageProgressiveLoad option is used and the image is
  172. * downloading. This block is thus called repeatedly with a partial image. When image is fully downloaded, the
  173. * block is called a last time with the full image and the last parameter set to YES.
  174. *
  175. * The last parameter is the original image URL
  176. *
  177. * @return Returns an instance of SDWebImageCombinedOperation, which you can cancel the loading process.
  178. */
  179. - (nullable SDWebImageCombinedOperation *)loadImageWithURL:(nullable NSURL *)url
  180. options:(SDWebImageOptions)options
  181. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  182. completed:(nonnull SDInternalCompletionBlock)completedBlock;
  183. /**
  184. * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  185. *
  186. * @param url The URL to the image
  187. * @param options A mask to specify options to use for this request
  188. * @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.
  189. * @param progressBlock A block called while image is downloading
  190. * @note the progress block is executed on a background queue
  191. * @param completedBlock A block called when operation has been completed.
  192. *
  193. * @return Returns an instance of SDWebImageCombinedOperation, which you can cancel the loading process.
  194. */
  195. - (nullable SDWebImageCombinedOperation *)loadImageWithURL:(nullable NSURL *)url
  196. options:(SDWebImageOptions)options
  197. context:(nullable SDWebImageContext *)context
  198. progress:(nullable SDImageLoaderProgressBlock)progressBlock
  199. completed:(nonnull SDInternalCompletionBlock)completedBlock;
  200. /**
  201. * Cancel all current operations
  202. */
  203. - (void)cancelAll;
  204. /**
  205. * Return the cache key for a given URL
  206. */
  207. - (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url;
  208. @end