SDImageCache.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 "SDImageCacheConfig.h"
  12. #import "SDImageCacheDefine.h"
  13. #import "SDMemoryCache.h"
  14. #import "SDDiskCache.h"
  15. /// Image Cache Options
  16. typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
  17. /**
  18. * By default, we do not query image data when the image is already cached in memory. This mask can force to query image data at the same time. However, this query is asynchronously unless you specify `SDImageCacheQueryMemoryDataSync`
  19. */
  20. SDImageCacheQueryMemoryData = 1 << 0,
  21. /**
  22. * By default, when you only specify `SDImageCacheQueryMemoryData`, we query the memory image data asynchronously. Combined this mask as well to query the memory image data synchronously.
  23. */
  24. SDImageCacheQueryMemoryDataSync = 1 << 1,
  25. /**
  26. * By default, when the memory cache miss, we query the disk cache asynchronously. This mask can force to query disk cache (when memory cache miss) synchronously.
  27. @note These 3 query options can be combined together. For the full list about these masks combination, see wiki page.
  28. */
  29. SDImageCacheQueryDiskDataSync = 1 << 2,
  30. /**
  31. * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
  32. * images to a size compatible with the constrained memory of devices.
  33. */
  34. SDImageCacheScaleDownLargeImages = 1 << 3,
  35. /**
  36. * 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.
  37. * 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.
  38. */
  39. SDImageCacheAvoidDecodeImage = 1 << 4,
  40. /**
  41. * By default, we decode the animated image. This flag can force decode the first frame only and produce the static image.
  42. */
  43. SDImageCacheDecodeFirstFrameOnly = 1 << 5,
  44. /**
  45. * 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 disk cache
  46. */
  47. SDImageCachePreloadAllFrames = 1 << 6,
  48. /**
  49. * By default, when you use `SDWebImageContextAnimatedImageClass` context option (like using `SDAnimatedImageView` which designed to use `SDAnimatedImage`), we may still use `UIImage` when the memory cache hit, or image decoder is not available, to behave as a fallback solution.
  50. * Using this option, can ensure we always produce image with your provided class. If failed, an error with code `SDWebImageErrorBadImageData` will be used.
  51. * Note this options is not compatible with `SDImageCacheDecodeFirstFrameOnly`, which always produce a UIImage/NSImage.
  52. */
  53. SDImageCacheMatchAnimatedImageClass = 1 << 7,
  54. };
  55. /**
  56. * SDImageCache maintains a memory cache and a disk cache. Disk cache write operations are performed
  57. * asynchronous so it doesn’t add unnecessary latency to the UI.
  58. */
  59. @interface SDImageCache : NSObject
  60. #pragma mark - Properties
  61. /**
  62. * Cache Config object - storing all kind of settings.
  63. * The property is copy so change of current config will not accidentally affect other cache's config.
  64. */
  65. @property (nonatomic, copy, nonnull, readonly) SDImageCacheConfig *config;
  66. /**
  67. * The memory cache implementation object used for current image cache.
  68. * By default we use `SDMemoryCache` class, you can also use this to call your own implementation class method.
  69. * @note To customize this class, check `SDImageCacheConfig.memoryCacheClass` property.
  70. */
  71. @property (nonatomic, strong, readonly, nonnull) id<SDMemoryCache> memoryCache;
  72. /**
  73. * The disk cache implementation object used for current image cache.
  74. * By default we use `SDMemoryCache` class, you can also use this to call your own implementation class method.
  75. * @note To customize this class, check `SDImageCacheConfig.diskCacheClass` property.
  76. * @warning When calling method about read/write in disk cache, be sure to either make your disk cache implementation IO-safe or using the same access queue to avoid issues.
  77. */
  78. @property (nonatomic, strong, readonly, nonnull) id<SDDiskCache> diskCache;
  79. /**
  80. * The disk cache's root path
  81. */
  82. @property (nonatomic, copy, nonnull, readonly) NSString *diskCachePath;
  83. /**
  84. * The additional disk cache path to check if the query from disk cache not exist;
  85. * The `key` param is the image cache key. The returned file path will be used to load the disk cache. If return nil, ignore it.
  86. * Useful if you want to bundle pre-loaded images with your app
  87. */
  88. @property (nonatomic, copy, nullable) SDImageCacheAdditionalCachePathBlock additionalCachePathBlock;
  89. #pragma mark - Singleton and initialization
  90. /**
  91. * Returns global shared cache instance
  92. */
  93. @property (nonatomic, class, readonly, nonnull) SDImageCache *sharedImageCache;
  94. /**
  95. * Control the default disk cache directory. This will effect all the SDImageCache instance created after modification, even for shared image cache.
  96. * This can be used to share the same disk cache with the App and App Extension (Today/Notification Widget) using `- [NSFileManager.containerURLForSecurityApplicationGroupIdentifier:]`.
  97. * @note If you pass nil, the value will be reset to `~/Library/Caches/com.hackemist.SDImageCache`.
  98. * @note We still preserve the `namespace` arg, which means, if you change this property into `/path/to/use`, the `SDImageCache.sharedImageCache.diskCachePath` should be `/path/to/use/default` because shared image cache use `default` as namespace.
  99. * Defaults to nil.
  100. */
  101. @property (nonatomic, class, readwrite, null_resettable) NSString *defaultDiskCacheDirectory;
  102. /**
  103. * Init a new cache store with a specific namespace
  104. * The final disk cache directory should looks like ($directory/$namespace). And the default config of shared cache, should result in (~/Library/Caches/com.hackemist.SDImageCache/default/)
  105. *
  106. * @param ns The namespace to use for this cache store
  107. */
  108. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;
  109. /**
  110. * Init a new cache store with a specific namespace and directory.
  111. * The final disk cache directory should looks like ($directory/$namespace). And the default config of shared cache, should result in (~/Library/Caches/com.hackemist.SDImageCache/default/)
  112. *
  113. * @param ns The namespace to use for this cache store
  114. * @param directory Directory to cache disk images in
  115. */
  116. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  117. diskCacheDirectory:(nullable NSString *)directory;
  118. /**
  119. * Init a new cache store with a specific namespace, directory and config.
  120. * The final disk cache directory should looks like ($directory/$namespace). And the default config of shared cache, should result in (~/Library/Caches/com.hackemist.SDImageCache/default/)
  121. *
  122. * @param ns The namespace to use for this cache store
  123. * @param directory Directory to cache disk images in
  124. * @param config The cache config to be used to create the cache. You can provide custom memory cache or disk cache class in the cache config
  125. */
  126. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  127. diskCacheDirectory:(nullable NSString *)directory
  128. config:(nullable SDImageCacheConfig *)config NS_DESIGNATED_INITIALIZER;
  129. #pragma mark - Cache paths
  130. /**
  131. Get the cache path for a certain key
  132. @param key The unique image cache key
  133. @return The cache path. You can check `lastPathComponent` to grab the file name.
  134. */
  135. - (nullable NSString *)cachePathForKey:(nullable NSString *)key;
  136. #pragma mark - Store Ops
  137. /**
  138. * Asynchronously store an image into memory and disk cache at the given key.
  139. *
  140. * @param image The image to store
  141. * @param key The unique image cache key, usually it's image absolute URL
  142. * @param completionBlock A block executed after the operation is finished
  143. */
  144. - (void)storeImage:(nullable UIImage *)image
  145. forKey:(nullable NSString *)key
  146. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  147. /**
  148. * Asynchronously store an image into memory and disk cache at the given key.
  149. *
  150. * @param image The image to store
  151. * @param key The unique image cache key, usually it's image absolute URL
  152. * @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
  153. * @param completionBlock A block executed after the operation is finished
  154. * @note If no image data is provided and encode to disk, we will try to detect the image format (using either `sd_imageFormat` or `SDAnimatedImage` protocol method) and animation status, to choose the best matched format, including GIF, JPEG or PNG.
  155. */
  156. - (void)storeImage:(nullable UIImage *)image
  157. forKey:(nullable NSString *)key
  158. toDisk:(BOOL)toDisk
  159. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  160. /**
  161. * Asynchronously store an image into memory and disk cache at the given key.
  162. *
  163. * @param image The image to store
  164. * @param imageData The image data as returned by the server, this representation will be used for disk storage
  165. * instead of converting the given image object into a storable/compressed image format in order
  166. * to save quality and CPU
  167. * @param key The unique image cache key, usually it's image absolute URL
  168. * @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
  169. * @param completionBlock A block executed after the operation is finished
  170. * @note If no image data is provided and encode to disk, we will try to detect the image format (using either `sd_imageFormat` or `SDAnimatedImage` protocol method) and animation status, to choose the best matched format, including GIF, JPEG or PNG.
  171. */
  172. - (void)storeImage:(nullable UIImage *)image
  173. imageData:(nullable NSData *)imageData
  174. forKey:(nullable NSString *)key
  175. toDisk:(BOOL)toDisk
  176. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  177. /**
  178. * Synchronously store image into memory cache at the given key.
  179. *
  180. * @param image The image to store
  181. * @param key The unique image cache key, usually it's image absolute URL
  182. */
  183. - (void)storeImageToMemory:(nullable UIImage*)image
  184. forKey:(nullable NSString *)key;
  185. /**
  186. * Synchronously store image data into disk cache at the given key.
  187. *
  188. * @param imageData The image data to store
  189. * @param key The unique image cache key, usually it's image absolute URL
  190. */
  191. - (void)storeImageDataToDisk:(nullable NSData *)imageData
  192. forKey:(nullable NSString *)key;
  193. #pragma mark - Contains and Check Ops
  194. /**
  195. * Asynchronously check if image exists in disk cache already (does not load the image)
  196. *
  197. * @param key the key describing the url
  198. * @param completionBlock the block to be executed when the check is done.
  199. * @note the completion block will be always executed on the main queue
  200. */
  201. - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDImageCacheCheckCompletionBlock)completionBlock;
  202. /**
  203. * Synchronously check if image data exists in disk cache already (does not load the image)
  204. *
  205. * @param key the key describing the url
  206. */
  207. - (BOOL)diskImageDataExistsWithKey:(nullable NSString *)key;
  208. #pragma mark - Query and Retrieve Ops
  209. /**
  210. * Synchronously query the image data for the given key in disk cache. You can decode the image data to image after loaded.
  211. *
  212. * @param key The unique key used to store the wanted image
  213. * @return The image data for the given key, or nil if not found.
  214. */
  215. - (nullable NSData *)diskImageDataForKey:(nullable NSString *)key;
  216. /**
  217. * Asynchronously query the image data for the given key in disk cache. You can decode the image data to image after loaded.
  218. *
  219. * @param key The unique key used to store the wanted image
  220. * @param completionBlock the block to be executed when the query is done.
  221. * @note the completion block will be always executed on the main queue
  222. */
  223. - (void)diskImageDataQueryForKey:(nullable NSString *)key completion:(nullable SDImageCacheQueryDataCompletionBlock)completionBlock;
  224. /**
  225. * Asynchronously queries the cache with operation and call the completion when done.
  226. *
  227. * @param key The unique key used to store the wanted image. If you want transformed or thumbnail image, calculate the key with `SDTransformedKeyForKey`, `SDThumbnailedKeyForKey`, or generate the cache key from url with `cacheKeyForURL:context:`.
  228. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  229. *
  230. * @return a NSOperation instance containing the cache op
  231. */
  232. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  233. /**
  234. * Asynchronously queries the cache with operation and call the completion when done.
  235. *
  236. * @param key The unique key used to store the wanted image. If you want transformed or thumbnail image, calculate the key with `SDTransformedKeyForKey`, `SDThumbnailedKeyForKey`, or generate the cache key from url with `cacheKeyForURL:context:`.
  237. * @param options A mask to specify options to use for this cache query
  238. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  239. *
  240. * @return a NSOperation instance containing the cache op
  241. */
  242. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  243. /**
  244. * Asynchronously queries the cache with operation and call the completion when done.
  245. *
  246. * @param key The unique key used to store the wanted image. If you want transformed or thumbnail image, calculate the key with `SDTransformedKeyForKey`, `SDThumbnailedKeyForKey`, or generate the cache key from url with `cacheKeyForURL:context:`.
  247. * @param options A mask to specify options to use for this cache query
  248. * @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.
  249. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  250. *
  251. * @return a NSOperation instance containing the cache op
  252. */
  253. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  254. /**
  255. * Asynchronously queries the cache with operation and call the completion when done.
  256. *
  257. * @param key The unique key used to store the wanted image. If you want transformed or thumbnail image, calculate the key with `SDTransformedKeyForKey`, `SDThumbnailedKeyForKey`, or generate the cache key from url with `cacheKeyForURL:context:`.
  258. * @param options A mask to specify options to use for this cache query
  259. * @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.
  260. * @param queryCacheType Specify where to query the cache from. By default we use `.all`, which means both memory cache and disk cache. You can choose to query memory only or disk only as well. Pass `.none` is invalid and callback with nil immediately.
  261. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  262. *
  263. * @return a NSOperation instance containing the cache op
  264. */
  265. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context cacheType:(SDImageCacheType)queryCacheType done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  266. /**
  267. * Synchronously query the memory cache.
  268. *
  269. * @param key The unique key used to store the image
  270. * @return The image for the given key, or nil if not found.
  271. */
  272. - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key;
  273. /**
  274. * Synchronously query the disk cache.
  275. *
  276. * @param key The unique key used to store the image
  277. * @return The image for the given key, or nil if not found.
  278. */
  279. - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key;
  280. /**
  281. * Synchronously query the disk cache. With the options and context which may effect the image generation. (Such as transformer, animated image, thumbnail, etc)
  282. *
  283. * @param key The unique key used to store the image
  284. * @param options A mask to specify options to use for this cache query
  285. * @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.
  286. * @return The image for the given key, or nil if not found.
  287. */
  288. - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context;
  289. /**
  290. * Synchronously query the cache (memory and or disk) after checking the memory cache.
  291. *
  292. * @param key The unique key used to store the image
  293. * @return The image for the given key, or nil if not found.
  294. */
  295. - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key;
  296. /**
  297. * Synchronously query the cache (memory and or disk) after checking the memory cache. With the options and context which may effect the image generation. (Such as transformer, animated image, thumbnail, etc)
  298. *
  299. * @param key The unique key used to store the image
  300. * @param options A mask to specify options to use for this cache query
  301. * @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.
  302. * @return The image for the given key, or nil if not found.
  303. */
  304. - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context;;
  305. #pragma mark - Remove Ops
  306. /**
  307. * Asynchronously remove the image from memory and disk cache
  308. *
  309. * @param key The unique image cache key
  310. * @param completion A block that should be executed after the image has been removed (optional)
  311. */
  312. - (void)removeImageForKey:(nullable NSString *)key withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  313. /**
  314. * Asynchronously remove the image from memory and optionally disk cache
  315. *
  316. * @param key The unique image cache key
  317. * @param fromDisk Also remove cache entry from disk if YES. If NO, the completion block is called synchronously
  318. * @param completion A block that should be executed after the image has been removed (optional)
  319. */
  320. - (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  321. /**
  322. Synchronously remove the image from memory cache.
  323. @param key The unique image cache key
  324. */
  325. - (void)removeImageFromMemoryForKey:(nullable NSString *)key;
  326. /**
  327. Synchronously remove the image from disk cache.
  328. @param key The unique image cache key
  329. */
  330. - (void)removeImageFromDiskForKey:(nullable NSString *)key;
  331. #pragma mark - Cache clean Ops
  332. /**
  333. * Synchronously Clear all memory cached images
  334. */
  335. - (void)clearMemory;
  336. /**
  337. * Asynchronously clear all disk cached images. Non-blocking method - returns immediately.
  338. * @param completion A block that should be executed after cache expiration completes (optional)
  339. */
  340. - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
  341. /**
  342. * Asynchronously remove all expired cached image from disk. Non-blocking method - returns immediately.
  343. * @param completionBlock A block that should be executed after cache expiration completes (optional)
  344. */
  345. - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock;
  346. #pragma mark - Cache Info
  347. /**
  348. * Get the total bytes size of images in the disk cache
  349. */
  350. - (NSUInteger)totalDiskSize;
  351. /**
  352. * Get the number of images in the disk cache
  353. */
  354. - (NSUInteger)totalDiskCount;
  355. /**
  356. * Asynchronously calculate the disk cache's size.
  357. */
  358. - (void)calculateSizeWithCompletionBlock:(nullable SDImageCacheCalculateSizeBlock)completionBlock;
  359. @end
  360. /**
  361. * SDImageCache is the built-in image cache implementation for web image manager. It adopts `SDImageCache` protocol to provide the function for web image manager to use for image loading process.
  362. */
  363. @interface SDImageCache (SDImageCache) <SDImageCache>
  364. @end