SDImageCacheDefine.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 "SDWebImageOperation.h"
  11. #import "SDWebImageDefine.h"
  12. #import "SDImageCoder.h"
  13. /// Image Cache Type
  14. typedef NS_ENUM(NSInteger, SDImageCacheType) {
  15. /**
  16. * For query and contains op in response, means the image isn't available in the image cache
  17. * For op in request, this type is not available and take no effect.
  18. */
  19. SDImageCacheTypeNone,
  20. /**
  21. * For query and contains op in response, means the image was obtained from the disk cache.
  22. * For op in request, means process only disk cache.
  23. */
  24. SDImageCacheTypeDisk,
  25. /**
  26. * For query and contains op in response, means the image was obtained from the memory cache.
  27. * For op in request, means process only memory cache.
  28. */
  29. SDImageCacheTypeMemory,
  30. /**
  31. * For query and contains op in response, this type is not available and take no effect.
  32. * For op in request, means process both memory cache and disk cache.
  33. */
  34. SDImageCacheTypeAll
  35. };
  36. typedef void(^SDImageCacheCheckCompletionBlock)(BOOL isInCache);
  37. typedef void(^SDImageCacheQueryDataCompletionBlock)(NSData * _Nullable data);
  38. typedef void(^SDImageCacheCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize);
  39. typedef NSString * _Nullable (^SDImageCacheAdditionalCachePathBlock)(NSString * _Nonnull key);
  40. typedef void(^SDImageCacheQueryCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType);
  41. typedef void(^SDImageCacheContainsCompletionBlock)(SDImageCacheType containsCacheType);
  42. /**
  43. This is the built-in decoding process for image query from cache.
  44. @note If you want to implement your custom loader with `queryImageForKey:options:context:completion:` API, but also want to keep compatible with SDWebImage's behavior, you'd better use this to produce image.
  45. @param imageData The image data from the cache. Should not be nil
  46. @param cacheKey The image cache key from the input. Should not be nil
  47. @param options The options arg from the input
  48. @param context The context arg from the input
  49. @return The decoded image for current image data query from cache
  50. */
  51. FOUNDATION_EXPORT UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context);
  52. /// Get the decode options from the loading context options and cache key. This is the built-in translate between the web loading part to the decoding part (which does not depens on).
  53. /// @param context The options arg from the input
  54. /// @param options The context arg from the input
  55. /// @param cacheKey The image cache key from the input. Should not be nil
  56. FOUNDATION_EXPORT SDImageCoderOptions * _Nonnull SDGetDecodeOptionsFromContext(SDWebImageContext * _Nullable context, SDWebImageOptions options, NSString * _Nonnull cacheKey);
  57. /**
  58. This is the image cache protocol to provide custom image cache for `SDWebImageManager`.
  59. Though the best practice to custom image cache, is to write your own class which conform `SDMemoryCache` or `SDDiskCache` protocol for `SDImageCache` class (See more on `SDImageCacheConfig.memoryCacheClass & SDImageCacheConfig.diskCacheClass`).
  60. However, if your own cache implementation contains more advanced feature beyond `SDImageCache` itself, you can consider to provide this instead. For example, you can even use a cache manager like `SDImageCachesManager` to register multiple caches.
  61. */
  62. @protocol SDImageCache <NSObject>
  63. @required
  64. /**
  65. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  66. If image is cached in memory, completion is called synchronously, else asynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  67. @param key The image cache key
  68. @param options A mask to specify options to use for this query
  69. @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.
  70. @param completionBlock The completion block. Will not get called if the operation is cancelled
  71. @return The operation for this query
  72. */
  73. - (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  74. options:(SDWebImageOptions)options
  75. context:(nullable SDWebImageContext *)context
  76. completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock;
  77. /**
  78. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  79. If image is cached in memory, completion is called synchronously, else asynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  80. @param key The image cache key
  81. @param options A mask to specify options to use for this query
  82. @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.
  83. @param cacheType 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.
  84. @param completionBlock The completion block. Will not get called if the operation is cancelled
  85. @return The operation for this query
  86. */
  87. - (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  88. options:(SDWebImageOptions)options
  89. context:(nullable SDWebImageContext *)context
  90. cacheType:(SDImageCacheType)cacheType
  91. completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock;
  92. /**
  93. Store the image into image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  94. @param image The image to store
  95. @param imageData The image data to be used for disk storage
  96. @param key The image cache key
  97. @param cacheType The image store op cache type
  98. @param completionBlock A block executed after the operation is finished
  99. */
  100. - (void)storeImage:(nullable UIImage *)image
  101. imageData:(nullable NSData *)imageData
  102. forKey:(nullable NSString *)key
  103. cacheType:(SDImageCacheType)cacheType
  104. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  105. /**
  106. Remove the image from image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  107. @param key The image cache key
  108. @param cacheType The image remove op cache type
  109. @param completionBlock A block executed after the operation is finished
  110. */
  111. - (void)removeImageForKey:(nullable NSString *)key
  112. cacheType:(SDImageCacheType)cacheType
  113. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  114. /**
  115. Check if image cache contains the image for the given key (does not load the image). If image is cached in memory, completion is called synchronously, else asynchronously.
  116. @param key The image cache key
  117. @param cacheType The image contains op cache type
  118. @param completionBlock A block executed after the operation is finished.
  119. */
  120. - (void)containsImageForKey:(nullable NSString *)key
  121. cacheType:(SDImageCacheType)cacheType
  122. completion:(nullable SDImageCacheContainsCompletionBlock)completionBlock;
  123. /**
  124. Clear all the cached images for image cache. If cache type is memory only, completion is called synchronously, else asynchronously.
  125. @param cacheType The image clear op cache type
  126. @param completionBlock A block executed after the operation is finished
  127. */
  128. - (void)clearWithCacheType:(SDImageCacheType)cacheType
  129. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  130. @end