SDImageCache.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /// Image Cache Options
  14. typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
  15. /**
  16. * 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`
  17. */
  18. SDImageCacheQueryMemoryData = 1 << 0,
  19. /**
  20. * 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.
  21. */
  22. SDImageCacheQueryMemoryDataSync = 1 << 1,
  23. /**
  24. * 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.
  25. @note These 3 query options can be combined together. For the full list about these masks combination, see wiki page.
  26. */
  27. SDImageCacheQueryDiskDataSync = 1 << 2,
  28. /**
  29. * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
  30. * images to a size compatible with the constrained memory of devices.
  31. */
  32. SDImageCacheScaleDownLargeImages = 1 << 3,
  33. /**
  34. * 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.
  35. * 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.
  36. */
  37. SDImageCacheAvoidDecodeImage = 1 << 4,
  38. /**
  39. * By default, we decode the animated image. This flag can force decode the first frame only and produece the static image.
  40. */
  41. SDImageCacheDecodeFirstFrameOnly = 1 << 5,
  42. /**
  43. * 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
  44. */
  45. SDImageCachePreloadAllFrames = 1 << 6
  46. };
  47. /**
  48. * SDImageCache maintains a memory cache and a disk cache. Disk cache write operations are performed
  49. * asynchronous so it doesn’t add unnecessary latency to the UI.
  50. */
  51. @interface SDImageCache : NSObject
  52. #pragma mark - Properties
  53. /**
  54. * Cache Config object - storing all kind of settings.
  55. * The property is copy so change of currrent config will not accidentally affect other cache's config.
  56. */
  57. @property (nonatomic, copy, nonnull, readonly) SDImageCacheConfig *config;
  58. /**
  59. * The disk cache's root path
  60. */
  61. @property (nonatomic, copy, nonnull, readonly) NSString *diskCachePath;
  62. /**
  63. * The additional disk cache path to check if the query from disk cache not exist;
  64. * 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.
  65. * Useful if you want to bundle pre-loaded images with your app
  66. */
  67. @property (nonatomic, copy, nullable) SDImageCacheAdditionalCachePathBlock additionalCachePathBlock;
  68. #pragma mark - Singleton and initialization
  69. /**
  70. * Returns global shared cache instance
  71. */
  72. @property (nonatomic, class, readonly, nonnull) SDImageCache *sharedImageCache;
  73. /**
  74. * Init a new cache store with a specific namespace
  75. *
  76. * @param ns The namespace to use for this cache store
  77. */
  78. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;
  79. /**
  80. * Init a new cache store with a specific namespace and directory.
  81. * If you don't provide the disk cache directory, we will use the User Cache directory with prefix (~/Library/Caches/com.hackemist.SDImageCache/).
  82. *
  83. * @param ns The namespace to use for this cache store
  84. * @param directory Directory to cache disk images in
  85. */
  86. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  87. diskCacheDirectory:(nullable NSString *)directory;
  88. /**
  89. * Init a new cache store with a specific namespace, directory and file manager
  90. * 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/)
  91. *
  92. * @param ns The namespace to use for this cache store
  93. * @param directory Directory to cache disk images in
  94. * @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
  95. */
  96. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  97. diskCacheDirectory:(nullable NSString *)directory
  98. config:(nullable SDImageCacheConfig *)config NS_DESIGNATED_INITIALIZER;
  99. #pragma mark - Cache paths
  100. /**
  101. Get the cache path for a certain key
  102. @param key The unique image cache key
  103. @return The cache path. You can check `lastPathComponent` to grab the file name.
  104. */
  105. - (nullable NSString *)cachePathForKey:(nullable NSString *)key;
  106. #pragma mark - Store Ops
  107. /**
  108. * Asynchronously store an image into memory and disk cache at the given key.
  109. *
  110. * @param image The image to store
  111. * @param key The unique image cache key, usually it's image absolute URL
  112. * @param completionBlock A block executed after the operation is finished
  113. */
  114. - (void)storeImage:(nullable UIImage *)image
  115. forKey:(nullable NSString *)key
  116. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  117. /**
  118. * Asynchronously store an image into memory and disk cache at the given key.
  119. *
  120. * @param image The image to store
  121. * @param key The unique image cache key, usually it's image absolute URL
  122. * @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
  123. * @param completionBlock A block executed after the operation is finished
  124. */
  125. - (void)storeImage:(nullable UIImage *)image
  126. forKey:(nullable NSString *)key
  127. toDisk:(BOOL)toDisk
  128. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  129. /**
  130. * Asynchronously store an image into memory and disk cache at the given key.
  131. *
  132. * @param image The image to store
  133. * @param imageData The image data as returned by the server, this representation will be used for disk storage
  134. * instead of converting the given image object into a storable/compressed image format in order
  135. * to save quality and CPU
  136. * @param key The unique image cache key, usually it's image absolute URL
  137. * @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
  138. * @param completionBlock A block executed after the operation is finished
  139. */
  140. - (void)storeImage:(nullable UIImage *)image
  141. imageData:(nullable NSData *)imageData
  142. forKey:(nullable NSString *)key
  143. toDisk:(BOOL)toDisk
  144. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  145. /**
  146. * Synchronously store image into memory cache at the given key.
  147. *
  148. * @param image The image to store
  149. * @param key The unique image cache key, usually it's image absolute URL
  150. */
  151. - (void)storeImageToMemory:(nullable UIImage*)image
  152. forKey:(nullable NSString *)key;
  153. /**
  154. * Synchronously store image data into disk cache at the given key.
  155. *
  156. * @param imageData The image data to store
  157. * @param key The unique image cache key, usually it's image absolute URL
  158. */
  159. - (void)storeImageDataToDisk:(nullable NSData *)imageData
  160. forKey:(nullable NSString *)key;
  161. #pragma mark - Contains and Check Ops
  162. /**
  163. * Asynchronously check if image exists in disk cache already (does not load the image)
  164. *
  165. * @param key the key describing the url
  166. * @param completionBlock the block to be executed when the check is done.
  167. * @note the completion block will be always executed on the main queue
  168. */
  169. - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDImageCacheCheckCompletionBlock)completionBlock;
  170. /**
  171. * Synchronously check if image data exists in disk cache already (does not load the image)
  172. *
  173. * @param key the key describing the url
  174. */
  175. - (BOOL)diskImageDataExistsWithKey:(nullable NSString *)key;
  176. #pragma mark - Query and Retrieve Ops
  177. /**
  178. * Asynchronously queries the cache with operation and call the completion when done.
  179. * Query the image data for the given key synchronously.
  180. *
  181. * @param key The unique key used to store the wanted image
  182. * @return The image data for the given key, or nil if not found.
  183. */
  184. - (nullable NSData *)diskImageDataForKey:(nullable NSString *)key;
  185. /**
  186. * Operation that queries the cache asynchronously and call the completion when done.
  187. *
  188. * @param key The unique key used to store the wanted image
  189. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  190. *
  191. * @return a NSOperation instance containing the cache op
  192. */
  193. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  194. /**
  195. * Asynchronously queries the cache with operation and call the completion when done.
  196. *
  197. * @param key The unique key used to store the wanted image
  198. * @param options A mask to specify options to use for this cache query
  199. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  200. *
  201. * @return a NSOperation instance containing the cache op
  202. */
  203. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  204. /**
  205. * Asynchronously queries the cache with operation and call the completion when done.
  206. *
  207. * @param key The unique key used to store the wanted image
  208. * @param options A mask to specify options to use for this cache query
  209. * @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.
  210. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  211. *
  212. * @return a NSOperation instance containing the cache op
  213. */
  214. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context done:(nullable SDImageCacheQueryCompletionBlock)doneBlock;
  215. /**
  216. * Synchronously query the memory cache.
  217. *
  218. * @param key The unique key used to store the image
  219. * @return The image for the given key, or nil if not found.
  220. */
  221. - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key;
  222. /**
  223. * Synchronously query the disk cache.
  224. *
  225. * @param key The unique key used to store the image
  226. * @return The image for the given key, or nil if not found.
  227. */
  228. - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key;
  229. /**
  230. * Synchronously query the cache (memory and or disk) after checking the memory cache.
  231. *
  232. * @param key The unique key used to store the image
  233. * @return The image for the given key, or nil if not found.
  234. */
  235. - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key;
  236. #pragma mark - Remove Ops
  237. /**
  238. * Asynchronously remove the image from memory and disk cache
  239. *
  240. * @param key The unique image cache key
  241. * @param completion A block that should be executed after the image has been removed (optional)
  242. */
  243. - (void)removeImageForKey:(nullable NSString *)key withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  244. /**
  245. * Asynchronously remove the image from memory and optionally disk cache
  246. *
  247. * @param key The unique image cache key
  248. * @param fromDisk Also remove cache entry from disk if YES. If NO, the completion block is called synchronously
  249. * @param completion A block that should be executed after the image has been removed (optional)
  250. */
  251. - (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  252. /**
  253. Synchronously remove the image from memory cache.
  254. @param key The unique image cache key
  255. */
  256. - (void)removeImageFromMemoryForKey:(nullable NSString *)key;
  257. /**
  258. Synchronously remove the image from disk cache.
  259. @param key The unique image cache key
  260. */
  261. - (void)removeImageFromDiskForKey:(nullable NSString *)key;
  262. #pragma mark - Cache clean Ops
  263. /**
  264. * Synchronously Clear all memory cached images
  265. */
  266. - (void)clearMemory;
  267. /**
  268. * Asynchronously clear all disk cached images. Non-blocking method - returns immediately.
  269. * @param completion A block that should be executed after cache expiration completes (optional)
  270. */
  271. - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
  272. /**
  273. * Asynchronously remove all expired cached image from disk. Non-blocking method - returns immediately.
  274. * @param completionBlock A block that should be executed after cache expiration completes (optional)
  275. */
  276. - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock;
  277. #pragma mark - Cache Info
  278. /**
  279. * Get the total bytes size of images in the disk cache
  280. */
  281. - (NSUInteger)totalDiskSize;
  282. /**
  283. * Get the number of images in the disk cache
  284. */
  285. - (NSUInteger)totalDiskCount;
  286. /**
  287. * Asynchronously calculate the disk cache's size.
  288. */
  289. - (void)calculateSizeWithCompletionBlock:(nullable SDImageCacheCalculateSizeBlock)completionBlock;
  290. @end
  291. /**
  292. * 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.
  293. */
  294. @interface SDImageCache (SDImageCache) <SDImageCache>
  295. @end