SDWebImageManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 "SDWebImageDownloader.h"
  11. #import "SDImageCache.h"
  12. typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
  13. /**
  14. * By default, when a URL fail to be downloaded, the URL is blacklisted so the library won't keep trying.
  15. * This flag disable this blacklisting.
  16. */
  17. SDWebImageRetryFailed = 1 << 0,
  18. /**
  19. * By default, image downloads are started during UI interactions, this flags disable this feature,
  20. * leading to delayed download on UIScrollView deceleration for instance.
  21. */
  22. SDWebImageLowPriority = 1 << 1,
  23. /**
  24. * This flag disables on-disk caching
  25. */
  26. SDWebImageCacheMemoryOnly = 1 << 2,
  27. /**
  28. * This flag enables progressive download, the image is displayed progressively during download as a browser would do.
  29. * By default, the image is only displayed once completely downloaded.
  30. */
  31. SDWebImageProgressiveDownload = 1 << 3,
  32. /**
  33. * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
  34. * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
  35. * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
  36. * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
  37. *
  38. * Use this flag only if you can't make your URLs static with embeded cache busting parameter.
  39. */
  40. SDWebImageRefreshCached = 1 << 4,
  41. /**
  42. * In iOS 4+, continue the download of the image if the app goes to background. This is achieved by asking the system for
  43. * extra time in background to let the request finish. If the background task expires the operation will be cancelled.
  44. */
  45. SDWebImageContinueInBackground = 1 << 5,
  46. /**
  47. * Handles cookies stored in NSHTTPCookieStore by setting
  48. * NSMutableURLRequest.HTTPShouldHandleCookies = YES;
  49. */
  50. SDWebImageHandleCookies = 1 << 6,
  51. /**
  52. * Enable to allow untrusted SSL ceriticates.
  53. * Useful for testing purposes. Use with caution in production.
  54. */
  55. SDWebImageAllowInvalidSSLCertificates = 1 << 7,
  56. /**
  57. * By default, image are loaded in the order they were queued. This flag move them to
  58. * the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which
  59. * could take a while).
  60. */
  61. SDWebImageHighPriority = 1 << 8,
  62. /**
  63. * By default, placeholder images are loaded while the image is loading. This flag will delay the loading
  64. * of the placeholder image until after the image has finished loading.
  65. */
  66. SDWebImageDelayPlaceholder = 1 << 9,
  67. /**
  68. * We usually don't call transformDownloadedImage delegate method on animated images,
  69. * as most transformation code would mangle it.
  70. * Use this flag to transform them anyway.
  71. */
  72. SDWebImageTransformAnimatedImage = 1 << 10,
  73. };
  74. typedef void(^SDWebImageCompletionBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL);
  75. typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL);
  76. typedef NSString *(^SDWebImageCacheKeyFilterBlock)(NSURL *url);
  77. @class SDWebImageManager;
  78. @protocol SDWebImageManagerDelegate <NSObject>
  79. @optional
  80. /**
  81. * Controls which image should be downloaded when the image is not found in the cache.
  82. *
  83. * @param imageManager The current `SDWebImageManager`
  84. * @param imageURL The url of the image to be downloaded
  85. *
  86. * @return Return NO to prevent the downloading of the image on cache misses. If not implemented, YES is implied.
  87. */
  88. - (BOOL)imageManager:(SDWebImageManager *)imageManager shouldDownloadImageForURL:(NSURL *)imageURL;
  89. /**
  90. * Allows to transform the image immediately after it has been downloaded and just before to cache it on disk and memory.
  91. * NOTE: This method is called from a global queue in order to not to block the main thread.
  92. *
  93. * @param imageManager The current `SDWebImageManager`
  94. * @param image The image to transform
  95. * @param imageURL The url of the image to transform
  96. *
  97. * @return The transformed image object.
  98. */
  99. - (UIImage *)imageManager:(SDWebImageManager *)imageManager transformDownloadedImage:(UIImage *)image withURL:(NSURL *)imageURL;
  100. @end
  101. /**
  102. * The SDWebImageManager is the class behind the UIImageView+WebCache category and likes.
  103. * It ties the asynchronous downloader (SDWebImageDownloader) with the image cache store (SDImageCache).
  104. * You can use this class directly to benefit from web image downloading with caching in another context than
  105. * a UIView.
  106. *
  107. * Here is a simple example of how to use SDWebImageManager:
  108. *
  109. * @code
  110. SDWebImageManager *manager = [SDWebImageManager sharedManager];
  111. [manager downloadWithURL:imageURL
  112. options:0
  113. progress:nil
  114. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  115. if (image) {
  116. // do something with image
  117. }
  118. }];
  119. * @endcode
  120. */
  121. @interface SDWebImageManager : NSObject
  122. @property (weak, nonatomic) id <SDWebImageManagerDelegate> delegate;
  123. @property (strong, nonatomic, readonly) SDImageCache *imageCache;
  124. @property (strong, nonatomic, readonly) SDWebImageDownloader *imageDownloader;
  125. /**
  126. * The cache filter is a block used each time SDWebImageManager need to convert an URL into a cache key. This can
  127. * be used to remove dynamic part of an image URL.
  128. *
  129. * The following example sets a filter in the application delegate that will remove any query-string from the
  130. * URL before to use it as a cache key:
  131. *
  132. * @code
  133. [[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url) {
  134. url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
  135. return [url absoluteString];
  136. }];
  137. * @endcode
  138. */
  139. @property (nonatomic, copy) SDWebImageCacheKeyFilterBlock cacheKeyFilter;
  140. /**
  141. * Returns global SDWebImageManager instance.
  142. *
  143. * @return SDWebImageManager shared instance
  144. */
  145. + (SDWebImageManager *)sharedManager;
  146. /**
  147. * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  148. *
  149. * @param url The URL to the image
  150. * @param options A mask to specify options to use for this request
  151. * @param progressBlock A block called while image is downloading
  152. * @param completedBlock A block called when operation has been completed.
  153. *
  154. * This parameter is required.
  155. *
  156. * This block has no return value and takes the requested UIImage as first parameter.
  157. * In case of error the image parameter is nil and the second parameter may contain an NSError.
  158. *
  159. * The third parameter is an `SDImageCacheType` enum indicating if the image was retrived from the local cache
  160. * or from the memory cache or from the network.
  161. *
  162. * The last parameter is set to NO when the SDWebImageProgressiveDownload option is used and the image is
  163. * downloading. This block is thus called repetidly with a partial image. When image is fully downloaded, the
  164. * block is called a last time with the full image and the last parameter set to YES.
  165. *
  166. * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation
  167. */
  168. - (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
  169. options:(SDWebImageOptions)options
  170. progress:(SDWebImageDownloaderProgressBlock)progressBlock
  171. completed:(SDWebImageCompletionWithFinishedBlock)completedBlock;
  172. /**
  173. * Saves image to cache for given URL
  174. *
  175. * @param image The image to cache
  176. * @param url The URL to the image
  177. *
  178. */
  179. - (void)saveImageToCache:(UIImage *)image forURL:(NSURL *)url;
  180. /**
  181. * Cancel all current opreations
  182. */
  183. - (void)cancelAll;
  184. /**
  185. * Check one or more operations running
  186. */
  187. - (BOOL)isRunning;
  188. /**
  189. * Check if image has already been cached
  190. *
  191. * @param url image url
  192. *
  193. * @return if the image was already cached
  194. */
  195. - (BOOL)cachedImageExistsForURL:(NSURL *)url;
  196. /**
  197. * Check if image has already been cached on disk only
  198. *
  199. * @param url image url
  200. *
  201. * @return if the image was already cached (disk only)
  202. */
  203. - (BOOL)diskImageExistsForURL:(NSURL *)url;
  204. /**
  205. * Async check if image has already been cached
  206. *
  207. * @param url image url
  208. * @param completionBlock the block to be executed when the check is finished
  209. *
  210. * @note the completion block is always executed on the main queue
  211. */
  212. - (void)cachedImageExistsForURL:(NSURL *)url
  213. completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;
  214. /**
  215. * Async check if image has already been cached on disk only
  216. *
  217. * @param url image url
  218. * @param completionBlock the block to be executed when the check is finished
  219. *
  220. * @note the completion block is always executed on the main queue
  221. */
  222. - (void)diskImageExistsForURL:(NSURL *)url
  223. completion:(SDWebImageCheckCacheCompletionBlock)completionBlock;
  224. /**
  225. *Return the cache key for a given URL
  226. */
  227. - (NSString *)cacheKeyForURL:(NSURL *)url;
  228. @end
  229. #pragma mark - Deprecated
  230. typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType) __deprecated_msg("Block type deprecated. Use `SDWebImageCompletionBlock`");
  231. typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) __deprecated_msg("Block type deprecated. Use `SDWebImageCompletionWithFinishedBlock`");
  232. @interface SDWebImageManager (Deprecated)
  233. /**
  234. * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  235. *
  236. * @deprecated This method has been deprecated. Use `downloadImageWithURL:options:progress:completed:`
  237. */
  238. - (id <SDWebImageOperation>)downloadWithURL:(NSURL *)url
  239. options:(SDWebImageOptions)options
  240. progress:(SDWebImageDownloaderProgressBlock)progressBlock
  241. completed:(SDWebImageCompletedWithFinishedBlock)completedBlock __deprecated_msg("Method deprecated. Use `downloadImageWithURL:options:progress:completed:`");
  242. @end