BU_SDImageCacheDefine.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 "BU_SDWebImageCompat.h"
  10. #import "BU_SDWebImageOperation.h"
  11. #import "BU_SDWebImageDefine.h"
  12. /// Image Cache Type
  13. typedef NS_ENUM(NSInteger, BU_SDImageCacheType) {
  14. /**
  15. * For query and contains op in response, means the image isn't available in the image cache
  16. * For op in request, this type is not available and take no effect.
  17. */
  18. BU_SDImageCacheTypeNone,
  19. /**
  20. * For query and contains op in response, means the image was obtained from the disk cache.
  21. * For op in request, means process only disk cache.
  22. */
  23. BU_SDImageCacheTypeDisk,
  24. /**
  25. * For query and contains op in response, means the image was obtained from the memory cache.
  26. * For op in request, means process only memory cache.
  27. */
  28. BU_SDImageCacheTypeMemory,
  29. /**
  30. * For query and contains op in response, this type is not available and take no effect.
  31. * For op in request, means process both memory cache and disk cache.
  32. */
  33. BU_SDImageCacheTypeAll
  34. };
  35. typedef void(^SDImageCacheCheckCompletionBlock)(BOOL isInCache);
  36. typedef void(^SDImageCacheCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize);
  37. typedef NSString * _Nullable (^SDImageCacheAdditionalCachePathBlock)(NSString * _Nonnull key);
  38. typedef void(^BU_SDImageCacheQueryCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, BU_SDImageCacheType cacheType);
  39. typedef void(^BU_SDImageCacheContainsCompletionBlock)(BU_SDImageCacheType containsCacheType);
  40. /**
  41. This is the built-in decoding process for image query from cache.
  42. @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.
  43. @param imageData The image data from the cache. Should not be nil
  44. @param cacheKey The image cache key from the input. Should not be nil
  45. @param options The options arg from the input
  46. @param context The context arg from the input
  47. @return The decoded image for current image data query from cache
  48. */
  49. FOUNDATION_EXPORT UIImage * _Nullable BU_SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, BU_SDWebImageOptions options, SDWebImageContext * _Nullable context);
  50. /**
  51. This is the image cache protocol to provide custom image cache for `SDWebImageManager`.
  52. 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`).
  53. 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.
  54. */
  55. @protocol BU_SDImageCache <NSObject>
  56. @required
  57. /**
  58. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  59. If image is cached in memory, completion is called synchronously, else aynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  60. @param key The image cache key
  61. @param options A mask to specify options to use for this query
  62. @param context A context contains different options to perform specify changes or processes, see `BU_SDWebImageContextOption`. This hold the extra objects which `options` enum can not hold.
  63. @param completionBlock The completion block. Will not get called if the operation is cancelled
  64. @return The operation for this query
  65. */
  66. - (nullable id<BU_SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  67. options:(BU_SDWebImageOptions)options
  68. context:(nullable SDWebImageContext *)context
  69. completion:(nullable BU_SDImageCacheQueryCompletionBlock)completionBlock;
  70. /**
  71. Store the image into image cache for the given key. If cache type is memory only, completion is called synchronously, else aynchronously.
  72. @param image The image to store
  73. @param imageData The image data to be used for disk storage
  74. @param key The image cache key
  75. @param cacheType The image store op cache type
  76. @param completionBlock A block executed after the operation is finished
  77. */
  78. - (void)storeImage:(nullable UIImage *)image
  79. imageData:(nullable NSData *)imageData
  80. forKey:(nullable NSString *)key
  81. cacheType:(BU_SDImageCacheType)cacheType
  82. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  83. /**
  84. Remove the image from image cache for the given key. If cache type is memory only, completion is called synchronously, else aynchronously.
  85. @param key The image cache key
  86. @param cacheType The image remove op cache type
  87. @param completionBlock A block executed after the operation is finished
  88. */
  89. - (void)removeImageForKey:(nullable NSString *)key
  90. cacheType:(BU_SDImageCacheType)cacheType
  91. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  92. /**
  93. 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 aynchronously.
  94. @param key The image cache key
  95. @param cacheType The image contains op cache type
  96. @param completionBlock A block executed after the operation is finished.
  97. */
  98. - (void)containsImageForKey:(nullable NSString *)key
  99. cacheType:(BU_SDImageCacheType)cacheType
  100. completion:(nullable BU_SDImageCacheContainsCompletionBlock)completionBlock;
  101. /**
  102. Clear all the cached images for image cache. If cache type is memory only, completion is called synchronously, else aynchronously.
  103. @param cacheType The image clear op cache type
  104. @param completionBlock A block executed after the operation is finished
  105. */
  106. - (void)clearWithCacheType:(BU_SDImageCacheType)cacheType
  107. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  108. @end