SDImageCacheDefine.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 depends on).
  53. /// @param context The context arg from the input
  54. /// @param options The options 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. /// Set the decode options to the loading context options. This is the built-in translate between the web loading part from the decoding part (which does not depends on).
  58. /// @param mutableContext The context arg to override
  59. /// @param mutableOptions The options arg to override
  60. /// @param decodeOptions The image decoding options
  61. FOUNDATION_EXPORT void SDSetDecodeOptionsToContext(SDWebImageMutableContext * _Nonnull mutableContext, SDWebImageOptions * _Nonnull mutableOptions, SDImageCoderOptions * _Nonnull decodeOptions);
  62. /**
  63. This is the image cache protocol to provide custom image cache for `SDWebImageManager`.
  64. 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`).
  65. 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.
  66. */
  67. @protocol SDImageCache <NSObject>
  68. @required
  69. /**
  70. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  71. If image is cached in memory, completion is called synchronously, else asynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  72. @param key The image cache key
  73. @param options A mask to specify options to use for this query
  74. @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. Pass `.callbackQueue` to control callback queue
  75. @param completionBlock The completion block. Will not get called if the operation is cancelled
  76. @return The operation for this query
  77. */
  78. - (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  79. options:(SDWebImageOptions)options
  80. context:(nullable SDWebImageContext *)context
  81. completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock API_DEPRECATED_WITH_REPLACEMENT("queryImageForKey:options:context:cacheType:completion:", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  82. @optional
  83. /**
  84. Query the cached image from image cache for given key. The operation can be used to cancel the query.
  85. If image is cached in memory, completion is called synchronously, else asynchronously and depends on the options arg (See `SDWebImageQueryDiskSync`)
  86. @param key The image cache key
  87. @param options A mask to specify options to use for this query
  88. @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. Pass `.callbackQueue` to control callback queue
  89. @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.
  90. @param completionBlock The completion block. Will not get called if the operation is cancelled
  91. @return The operation for this query
  92. */
  93. - (nullable id<SDWebImageOperation>)queryImageForKey:(nullable NSString *)key
  94. options:(SDWebImageOptions)options
  95. context:(nullable SDWebImageContext *)context
  96. cacheType:(SDImageCacheType)cacheType
  97. completion:(nullable SDImageCacheQueryCompletionBlock)completionBlock;
  98. @required
  99. /**
  100. Store the image into image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  101. @param image The image to store
  102. @param imageData The image data to be used for disk storage
  103. @param key The image cache key
  104. @param cacheType The image store op cache type
  105. @param completionBlock A block executed after the operation is finished
  106. */
  107. - (void)storeImage:(nullable UIImage *)image
  108. imageData:(nullable NSData *)imageData
  109. forKey:(nullable NSString *)key
  110. cacheType:(SDImageCacheType)cacheType
  111. completion:(nullable SDWebImageNoParamsBlock)completionBlock API_DEPRECATED_WITH_REPLACEMENT("storeImage:imageData:forKey:options:context:cacheType:completion:", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  112. @optional
  113. /**
  114. Store the image into image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  115. @param image The image to store
  116. @param imageData The image data to be used for disk storage
  117. @param key The image cache key
  118. @param options A mask to specify options to use for this store
  119. @param context The context options to use. Pass `.callbackQueue` to control callback queue
  120. @param cacheType The image store op cache type
  121. @param completionBlock A block executed after the operation is finished
  122. */
  123. - (void)storeImage:(nullable UIImage *)image
  124. imageData:(nullable NSData *)imageData
  125. forKey:(nullable NSString *)key
  126. options:(SDWebImageOptions)options
  127. context:(nullable SDWebImageContext *)context
  128. cacheType:(SDImageCacheType)cacheType
  129. completion:(nullable SDWebImageNoParamsBlock)completionBlock;
  130. #pragma mark - Deprecated because SDWebImageManager does not use these APIs
  131. /**
  132. Remove the image from image cache for the given key. If cache type is memory only, completion is called synchronously, else asynchronously.
  133. @param key The image cache key
  134. @param cacheType The image remove op cache type
  135. @param completionBlock A block executed after the operation is finished
  136. */
  137. - (void)removeImageForKey:(nullable NSString *)key
  138. cacheType:(SDImageCacheType)cacheType
  139. completion:(nullable SDWebImageNoParamsBlock)completionBlock API_DEPRECATED("No longer use. Cast to cache instance and call its API", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  140. /**
  141. 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.
  142. @param key The image cache key
  143. @param cacheType The image contains op cache type
  144. @param completionBlock A block executed after the operation is finished.
  145. */
  146. - (void)containsImageForKey:(nullable NSString *)key
  147. cacheType:(SDImageCacheType)cacheType
  148. completion:(nullable SDImageCacheContainsCompletionBlock)completionBlock API_DEPRECATED("No longer use. Cast to cache instance and call its API", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  149. /**
  150. Clear all the cached images for image cache. If cache type is memory only, completion is called synchronously, else asynchronously.
  151. @param cacheType The image clear op cache type
  152. @param completionBlock A block executed after the operation is finished
  153. */
  154. - (void)clearWithCacheType:(SDImageCacheType)cacheType
  155. completion:(nullable SDWebImageNoParamsBlock)completionBlock API_DEPRECATED("No longer use. Cast to cache instance and call its API", macos(10.10, API_TO_BE_DEPRECATED), ios(8.0, API_TO_BE_DEPRECATED), tvos(9.0, API_TO_BE_DEPRECATED), watchos(2.0, API_TO_BE_DEPRECATED));
  156. @end