YYImageCache.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. //
  2. // YYImageCache.h
  3. // YYKit <https://github.com/ibireme/YYKit>
  4. //
  5. // Created by ibireme on 15/2/15.
  6. // Copyright (c) 2015 ibireme.
  7. //
  8. // This source code is licensed under the MIT-style license found in the
  9. // LICENSE file in the root directory of this source tree.
  10. //
  11. #import <UIKit/UIKit.h>
  12. @class YYMemoryCache, YYDiskCache;
  13. NS_ASSUME_NONNULL_BEGIN
  14. /// Image cache type
  15. typedef NS_OPTIONS(NSUInteger, YYImageCacheType) {
  16. /// No value.
  17. YYImageCacheTypeNone = 0,
  18. /// Get/store image with memory cache.
  19. YYImageCacheTypeMemory = 1 << 0,
  20. /// Get/store image with disk cache.
  21. YYImageCacheTypeDisk = 1 << 1,
  22. /// Get/store image with both memory cache and disk cache.
  23. YYImageCacheTypeAll = YYImageCacheTypeMemory | YYImageCacheTypeDisk,
  24. };
  25. /**
  26. YYImageCache is a cache that stores UIImage and image data based on memory cache and disk cache.
  27. @discussion The disk cache will try to protect the original image data:
  28. * If the original image is still image, it will be saved as png/jpeg file based on alpha information.
  29. * If the original image is animated gif, apng or webp, it will be saved as original format.
  30. * If the original image's scale is not 1, the scale value will be saved as extended data.
  31. Although UIImage can be serialized with NSCoding protocol, but it's not a good idea:
  32. Apple actually use UIImagePNGRepresentation() to encode all kind of image, it may
  33. lose the original multi-frame data. The result is packed to plist file and cannot
  34. view with photo viewer directly. If the image has no alpha channel, using JPEG
  35. instead of PNG can save more disk size and encoding/decoding time.
  36. */
  37. @interface YYImageCache : NSObject
  38. #pragma mark - Attribute
  39. ///=============================================================================
  40. /// @name Attribute
  41. ///=============================================================================
  42. /** The name of the cache. Default is nil. */
  43. @property (nullable, copy) NSString *name;
  44. /** The underlying memory cache. see `YYMemoryCache` for more information.*/
  45. @property (strong, readonly) YYMemoryCache *memoryCache;
  46. /** The underlying disk cache. see `YYDiskCache` for more information.*/
  47. @property (strong, readonly) YYDiskCache *diskCache;
  48. /**
  49. Whether decode animated image when fetch image from disk cache. Default is YES.
  50. @discussion When fetch image from disk cache, it will use 'YYImage' to decode
  51. animated image such as WebP/APNG/GIF. Set to 'NO' to ignore animated image.
  52. */
  53. @property BOOL allowAnimatedImage;
  54. /**
  55. Whether decode the image to memory bitmap. Default is YES.
  56. @discussion If the value is YES, then the image will be decoded to memory bitmap
  57. for better display performance, but may cost more memory.
  58. */
  59. @property BOOL decodeForDisplay;
  60. #pragma mark - Initializer
  61. ///=============================================================================
  62. /// @name Initializer
  63. ///=============================================================================
  64. - (instancetype)init UNAVAILABLE_ATTRIBUTE;
  65. + (instancetype)new UNAVAILABLE_ATTRIBUTE;
  66. /**
  67. Returns global shared image cache instance.
  68. @return The singleton YYImageCache instance.
  69. */
  70. + (instancetype)sharedCache;
  71. /**
  72. The designated initializer. Multiple instances with the same path will make the
  73. cache unstable.
  74. @param path Full path of a directory in which the cache will write data.
  75. Once initialized you should not read and write to this directory.
  76. @result A new cache object, or nil if an error occurs.
  77. */
  78. - (nullable instancetype)initWithPath:(NSString *)path NS_DESIGNATED_INITIALIZER;
  79. #pragma mark - Access Methods
  80. ///=============================================================================
  81. /// @name Access Methods
  82. ///=============================================================================
  83. /**
  84. Sets the image with the specified key in the cache (both memory and disk).
  85. This method returns immediately and executes the store operation in background.
  86. @param image The image to be stored in the cache. If nil, this method has no effect.
  87. @param key The key with which to associate the image. If nil, this method has no effect.
  88. */
  89. - (void)setImage:(UIImage *)image forKey:(NSString *)key;
  90. /**
  91. Sets the image with the specified key in the cache.
  92. This method returns immediately and executes the store operation in background.
  93. @discussion If the `type` contain `YYImageCacheTypeMemory`, then the `image` will
  94. be stored in the memory cache; `imageData` will be used instead if `image` is nil.
  95. If the `type` contain `YYImageCacheTypeDisk`, then the `imageData` will
  96. be stored in the disk cache; `image` will be used instead if `imageData` is nil.
  97. @param image The image to be stored in the cache.
  98. @param imageData The image data to be stored in the cache.
  99. @param key The key with which to associate the image. If nil, this method has no effect.
  100. @param type The cache type to store image.
  101. */
  102. - (void)setImage:(nullable UIImage *)image
  103. imageData:(nullable NSData *)imageData
  104. forKey:(NSString *)key
  105. withType:(YYImageCacheType)type;
  106. /**
  107. Removes the image of the specified key in the cache (both memory and disk).
  108. This method returns immediately and executes the remove operation in background.
  109. @param key The key identifying the image to be removed. If nil, this method has no effect.
  110. */
  111. - (void)removeImageForKey:(NSString *)key;
  112. /**
  113. Removes the image of the specified key in the cache.
  114. This method returns immediately and executes the remove operation in background.
  115. @param key The key identifying the image to be removed. If nil, this method has no effect.
  116. @param type The cache type to remove image.
  117. */
  118. - (void)removeImageForKey:(NSString *)key withType:(YYImageCacheType)type;
  119. /**
  120. Returns a Boolean value that indicates whether a given key is in cache.
  121. If the image is not in memory, this method may blocks the calling thread until
  122. file read finished.
  123. @param key A string identifying the image. If nil, just return NO.
  124. @return Whether the image is in cache.
  125. */
  126. - (BOOL)containsImageForKey:(NSString *)key;
  127. /**
  128. Returns a Boolean value that indicates whether a given key is in cache.
  129. If the image is not in memory and the `type` contains `YYImageCacheTypeDisk`,
  130. this method may blocks the calling thread until file read finished.
  131. @param key A string identifying the image. If nil, just return NO.
  132. @param type The cache type.
  133. @return Whether the image is in cache.
  134. */
  135. - (BOOL)containsImageForKey:(NSString *)key withType:(YYImageCacheType)type;
  136. /**
  137. Returns the image associated with a given key.
  138. If the image is not in memory, this method may blocks the calling thread until
  139. file read finished.
  140. @param key A string identifying the image. If nil, just return nil.
  141. @return The image associated with key, or nil if no image is associated with key.
  142. */
  143. - (nullable UIImage *)getImageForKey:(NSString *)key;
  144. /**
  145. Returns the image associated with a given key.
  146. If the image is not in memory and the `type` contains `YYImageCacheTypeDisk`,
  147. this method may blocks the calling thread until file read finished.
  148. @param key A string identifying the image. If nil, just return nil.
  149. @return The image associated with key, or nil if no image is associated with key.
  150. */
  151. - (nullable UIImage *)getImageForKey:(NSString *)key withType:(YYImageCacheType)type;
  152. /**
  153. Asynchronously get the image associated with a given key.
  154. @param key A string identifying the image. If nil, just return nil.
  155. @param type The cache type.
  156. @param block A completion block which will be called on main thread.
  157. */
  158. - (void)getImageForKey:(NSString *)key
  159. withType:(YYImageCacheType)type
  160. withBlock:(void(^)(UIImage * _Nullable image, YYImageCacheType type))block;
  161. /**
  162. Returns the image data associated with a given key.
  163. This method may blocks the calling thread until file read finished.
  164. @param key A string identifying the image. If nil, just return nil.
  165. @return The image data associated with key, or nil if no image is associated with key.
  166. */
  167. - (nullable NSData *)getImageDataForKey:(NSString *)key;
  168. /**
  169. Asynchronously get the image data associated with a given key.
  170. @param key A string identifying the image. If nil, just return nil.
  171. @param block A completion block which will be called on main thread.
  172. */
  173. - (void)getImageDataForKey:(NSString *)key
  174. withBlock:(void(^)(NSData * _Nullable imageData))block;
  175. @end
  176. NS_ASSUME_NONNULL_END