SDImageCache.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 "SDImageCacheConfig.h"
  11. typedef NS_ENUM(NSInteger, SDImageCacheType) {
  12. /**
  13. * The image wasn't available the SDWebImage caches, but was downloaded from the web.
  14. */
  15. SDImageCacheTypeNone,
  16. /**
  17. * The image was obtained from the disk cache.
  18. */
  19. SDImageCacheTypeDisk,
  20. /**
  21. * The image was obtained from the memory cache.
  22. */
  23. SDImageCacheTypeMemory
  24. };
  25. typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
  26. /**
  27. * By default, we do not query disk data when the image is cached in memory. This mask can force to query disk data at the same time.
  28. */
  29. SDImageCacheQueryDataWhenInMemory = 1 << 0,
  30. /**
  31. * By default, we query the memory cache synchronously, disk cache asynchronously. This mask can force to query disk cache synchronously.
  32. */
  33. SDImageCacheQueryDiskSync = 1 << 1,
  34. /**
  35. * By default, images are decoded respecting their original size. On iOS, this flag will scale down the
  36. * images to a size compatible with the constrained memory of devices.
  37. */
  38. SDImageCacheScaleDownLargeImages = 1 << 2
  39. };
  40. typedef void(^SDCacheQueryCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType);
  41. typedef void(^SDWebImageCheckCacheCompletionBlock)(BOOL isInCache);
  42. typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize);
  43. /**
  44. * SDImageCache maintains a memory cache and an optional disk cache. Disk cache write operations are performed
  45. * asynchronous so it doesn’t add unnecessary latency to the UI.
  46. */
  47. @interface SDImageCache : NSObject
  48. #pragma mark - Properties
  49. /**
  50. * Cache Config object - storing all kind of settings
  51. */
  52. @property (nonatomic, nonnull, readonly) SDImageCacheConfig *config;
  53. /**
  54. * The maximum "total cost" of the in-memory image cache. The cost function is the number of pixels held in memory.
  55. */
  56. @property (assign, nonatomic) NSUInteger maxMemoryCost;
  57. /**
  58. * The maximum number of objects the cache should hold.
  59. */
  60. @property (assign, nonatomic) NSUInteger maxMemoryCountLimit;
  61. #pragma mark - Singleton and initialization
  62. /**
  63. * Returns global shared cache instance
  64. *
  65. * @return SDImageCache global instance
  66. */
  67. + (nonnull instancetype)sharedImageCache;
  68. /**
  69. * Init a new cache store with a specific namespace
  70. *
  71. * @param ns The namespace to use for this cache store
  72. */
  73. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns;
  74. /**
  75. * Init a new cache store with a specific namespace and directory
  76. *
  77. * @param ns The namespace to use for this cache store
  78. * @param directory Directory to cache disk images in
  79. */
  80. - (nonnull instancetype)initWithNamespace:(nonnull NSString *)ns
  81. diskCacheDirectory:(nonnull NSString *)directory NS_DESIGNATED_INITIALIZER;
  82. #pragma mark - Cache paths
  83. - (nullable NSString *)makeDiskCachePath:(nonnull NSString*)fullNamespace;
  84. /**
  85. * Add a read-only cache path to search for images pre-cached by SDImageCache
  86. * Useful if you want to bundle pre-loaded images with your app
  87. *
  88. * @param path The path to use for this read-only cache path
  89. */
  90. - (void)addReadOnlyCachePath:(nonnull NSString *)path;
  91. #pragma mark - Store Ops
  92. /**
  93. * Asynchronously store an image into memory and disk cache at the given key.
  94. *
  95. * @param image The image to store
  96. * @param key The unique image cache key, usually it's image absolute URL
  97. * @param completionBlock A block executed after the operation is finished
  98. */
  99. - (void)storeImage:(nullable UIImage *)image
  100. forKey:(nullable NSString *)key
  101. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  102. /**
  103. * Asynchronously store an image into memory and disk cache at the given key.
  104. *
  105. * @param image The image to store
  106. * @param key The unique image cache key, usually it's image absolute URL
  107. * @param toDisk Store the image to disk cache if YES
  108. * @param completionBlock A block executed after the operation is finished
  109. */
  110. - (void)storeImage:(nullable UIImage *)image
  111. forKey:(nullable NSString *)key
  112. toDisk:(BOOL)toDisk
  113. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  114. /**
  115. * Asynchronously store an image into memory and disk cache at the given key.
  116. *
  117. * @param image The image to store
  118. * @param imageData The image data as returned by the server, this representation will be used for disk storage
  119. * instead of converting the given image object into a storable/compressed image format in order
  120. * to save quality and CPU
  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
  123. * @param completionBlock A block executed after the operation is finished
  124. */
  125. - (void)storeImage:(nullable UIImage *)image
  126. imageData:(nullable NSData *)imageData
  127. forKey:(nullable NSString *)key
  128. toDisk:(BOOL)toDisk
  129. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  130. /**
  131. * Synchronously store image NSData into disk cache at the given key.
  132. *
  133. *
  134. * @param imageData The image data to store
  135. * @param key The unique image cache key, usually it's image absolute URL
  136. */
  137. - (void)storeImageDataToDisk:(nullable NSData *)imageData forKey:(nullable NSString *)key;
  138. #pragma mark - Query and Retrieve Ops
  139. /**
  140. * Async check if image exists in disk cache already (does not load the image)
  141. *
  142. * @param key the key describing the url
  143. * @param completionBlock the block to be executed when the check is done.
  144. * @note the completion block will be always executed on the main queue
  145. */
  146. - (void)diskImageExistsWithKey:(nullable NSString *)key completion:(nullable SDWebImageCheckCacheCompletionBlock)completionBlock;
  147. /**
  148. * Sync check if image data exists in disk cache already (does not load the image)
  149. *
  150. * @param key the key describing the url
  151. */
  152. - (BOOL)diskImageDataExistsWithKey:(nullable NSString *)key;
  153. /**
  154. * Query the image data for the given key synchronously.
  155. *
  156. * @param key The unique key used to store the wanted image
  157. * @return The image data for the given key, or nil if not found.
  158. */
  159. - (nullable NSData *)diskImageDataForKey:(nullable NSString *)key;
  160. /**
  161. * Operation that queries the cache asynchronously and call the completion when done.
  162. *
  163. * @param key The unique key used to store the wanted image
  164. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  165. *
  166. * @return a NSOperation instance containing the cache op
  167. */
  168. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key done:(nullable SDCacheQueryCompletedBlock)doneBlock;
  169. /**
  170. * Operation that queries the cache asynchronously and call the completion when done.
  171. *
  172. * @param key The unique key used to store the wanted image
  173. * @param options A mask to specify options to use for this cache query
  174. * @param doneBlock The completion block. Will not get called if the operation is cancelled
  175. *
  176. * @return a NSOperation instance containing the cache op
  177. */
  178. - (nullable NSOperation *)queryCacheOperationForKey:(nullable NSString *)key options:(SDImageCacheOptions)options done:(nullable SDCacheQueryCompletedBlock)doneBlock;
  179. /**
  180. * Query the memory cache synchronously.
  181. *
  182. * @param key The unique key used to store the image
  183. * @return The image for the given key, or nil if not found.
  184. */
  185. - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key;
  186. /**
  187. * Query the disk cache synchronously.
  188. *
  189. * @param key The unique key used to store the image
  190. * @return The image for the given key, or nil if not found.
  191. */
  192. - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key;
  193. /**
  194. * Query the cache (memory and or disk) synchronously after checking the memory cache.
  195. *
  196. * @param key The unique key used to store the image
  197. * @return The image for the given key, or nil if not found.
  198. */
  199. - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key;
  200. #pragma mark - Remove Ops
  201. /**
  202. * Remove the image from memory and disk cache asynchronously
  203. *
  204. * @param key The unique image cache key
  205. * @param completion A block that should be executed after the image has been removed (optional)
  206. */
  207. - (void)removeImageForKey:(nullable NSString *)key withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  208. /**
  209. * Remove the image from memory and optionally disk cache asynchronously
  210. *
  211. * @param key The unique image cache key
  212. * @param fromDisk Also remove cache entry from disk if YES
  213. * @param completion A block that should be executed after the image has been removed (optional)
  214. */
  215. - (void)removeImageForKey:(nullable NSString *)key fromDisk:(BOOL)fromDisk withCompletion:(nullable SDWebImageNoParamsBlock)completion;
  216. #pragma mark - Cache clean Ops
  217. /**
  218. * Clear all memory cached images
  219. */
  220. - (void)clearMemory;
  221. /**
  222. * Async clear all disk cached images. Non-blocking method - returns immediately.
  223. * @param completion A block that should be executed after cache expiration completes (optional)
  224. */
  225. - (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion;
  226. /**
  227. * Async remove all expired cached image from disk. Non-blocking method - returns immediately.
  228. * @param completionBlock A block that should be executed after cache expiration completes (optional)
  229. */
  230. - (void)deleteOldFilesWithCompletionBlock:(nullable SDWebImageNoParamsBlock)completionBlock;
  231. #pragma mark - Cache Info
  232. /**
  233. * Get the size used by the disk cache
  234. */
  235. - (NSUInteger)getSize;
  236. /**
  237. * Get the number of images in the disk cache
  238. */
  239. - (NSUInteger)getDiskCount;
  240. /**
  241. * Asynchronously calculate the disk cache's size.
  242. */
  243. - (void)calculateSizeWithCompletionBlock:(nullable SDWebImageCalculateSizeBlock)completionBlock;
  244. #pragma mark - Cache Paths
  245. /**
  246. * Get the cache path for a certain key (needs the cache path root folder)
  247. *
  248. * @param key the key (can be obtained from url using cacheKeyForURL)
  249. * @param path the cache path root folder
  250. *
  251. * @return the cache path
  252. */
  253. - (nullable NSString *)cachePathForKey:(nullable NSString *)key inPath:(nonnull NSString *)path;
  254. /**
  255. * Get the default cache path for a certain key
  256. *
  257. * @param key the key (can be obtained from url using cacheKeyForURL)
  258. *
  259. * @return the default cache path
  260. */
  261. - (nullable NSString *)defaultCachePathForKey:(nullable NSString *)key;
  262. @end