SDImageCacheDefine.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. /// Image Cache Type
  13. typedef NS_ENUM(NSInteger, 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. 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. 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. 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. SDImageCacheTypeAll
  34. };
  35. typedef void(^SDImageCacheCheckCompletionBlock)(BOOL isInCache);
  36. typedef void(^SDImageCacheQueryDataCompletionBlock)(NSData * _Nullable data);
  37. typedef void(^SDImageCacheCalculateSizeBlock)(NSUInteger fileCount, NSUInteger totalSize);
  38. typedef NSString * _Nullable (^SDImageCacheAdditionalCachePathBlock)(NSString * _Nonnull key);
  39. typedef void(^SDImageCacheQueryCompletionBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType);
  40. typedef void(^SDImageCacheContainsCompletionBlock)(SDImageCacheType containsCacheType);
  41. /**
  42. This is the built-in decoding process for image query from cache.
  43. @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.
  44. @param imageData The image data from the cache. Should not be nil
  45. @param cacheKey The image cache key from the input. Should not be nil
  46. @param options The options arg from the input
  47. @param context The context arg from the input
  48. @return The decoded image for current image data query from cache
  49. */
  50. FOUNDATION_EXPORT UIImage * _Nullable SDImageCacheDecodeImageData(NSData * _Nonnull imageData, NSString * _Nonnull cacheKey, SDWebImageOptions options, SDWebImageContext * _Nullable context);
  51. /**
  52. This is the image cache protocol to provide custom image cache for `SDWebImageManager`.
  53. 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`).
  54. 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.
  55. */
  56. @protocol SDImageCache <NSObject>
  57. @required
  58. /**
  59. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  60. If image is cached in memory, completion is called synchronously, else asynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  61. @param key The image cache key
  62. @param options A mask to specify options to use for this query
  63. @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.
  64. @param completionBlock The completion block. Will not get called if the operation is cancelled
  65. @return The operation for this query
  66. */
  67. - (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  68. options:(SDWebImageOptions)options
  69. context:(nullable SDWebImageContext *)context
  70. completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock;
  71. /**
  72. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  73. If image is cached in memory, completion is called synchronously, else asynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  74. @param key The image cache key
  75. @param options A mask to specify options to use for this query
  76. @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.
  77. @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.
  78. @param completionBlock The completion block. Will not get called if the operation is cancelled
  79. @return The operation for this query
  80. */
  81. - (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  82. options:(SDWebImageOptions)options
  83. context:(nullable SDWebImageContext *)context
  84. cacheType:(SDImageCacheType)cacheType
  85. completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock;
  86. /**
  87. Store the image into image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  88. @param image The image to store
  89. @param imageData The image data to be used for disk storage
  90. @param key The image cache key
  91. @param cacheType The image store op cache type
  92. @param completionBlock A block executed after the operation is finished
  93. */
  94. - (void)storeImage:(nullable UIImage *)image
  95. imageData:(nullable NSData *)imageData
  96. forKey:(nullable NSString *)key
  97. cacheType:(SDImageCacheType)cacheType
  98. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  99. /**
  100. Remove the image from image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  101. @param key The image cache key
  102. @param cacheType The image remove op cache type
  103. @param completionBlock A block executed after the operation is finished
  104. */
  105. - (void)removeImageForKey:(nullable NSString *)key
  106. cacheType:(SDImageCacheType)cacheType
  107. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  108. /**
  109. 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.
  110. @param key The image cache key
  111. @param cacheType The image contains op cache type
  112. @param completionBlock A block executed after the operation is finished.
  113. */
  114. - (void)containsImageForKey:(nullable NSString *)key
  115. cacheType:(SDImageCacheType)cacheType
  116. completion:(nullable SDImageCacheContainsCompletionBlock)completionBlock;
  117. /**
  118. Clear all the cached images for image cache. If cache type is memory only, completion is called synchronously, else asynchronously.
  119. @param cacheType The image clear op cache type
  120. @param completionBlock A block executed after the operation is finished
  121. */
  122. - (void)clearWithCacheType:(SDImageCacheType)cacheType
  123. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  124. @end