No Description

AFAutoPurgingImageCache.h 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // AFAutoPurgingImageCache.h
  2. // Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import <TargetConditionals.h>
  22. #import <Foundation/Foundation.h>
  23. #if TARGET_OS_IOS || TARGET_OS_TV
  24. #import <UIKit/UIKit.h>
  25. NS_ASSUME_NONNULL_BEGIN
  26. /**
  27. The `AFImageCache` protocol defines a set of APIs for adding, removing and fetching images from a cache synchronously.
  28. */
  29. @protocol AFImageCache <NSObject>
  30. /**
  31. Adds the image to the cache with the given identifier.
  32. @param image The image to cache.
  33. @param identifier The unique identifier for the image in the cache.
  34. */
  35. - (void)addImage:(UIImage *)image withIdentifier:(NSString *)identifier;
  36. /**
  37. Removes the image from the cache matching the given identifier.
  38. @param identifier The unique identifier for the image in the cache.
  39. @return A BOOL indicating whether or not the image was removed from the cache.
  40. */
  41. - (BOOL)removeImageWithIdentifier:(NSString *)identifier;
  42. /**
  43. Removes all images from the cache.
  44. @return A BOOL indicating whether or not all images were removed from the cache.
  45. */
  46. - (BOOL)removeAllImages;
  47. /**
  48. Returns the image in the cache associated with the given identifier.
  49. @param identifier The unique identifier for the image in the cache.
  50. @return An image for the matching identifier, or nil.
  51. */
  52. - (nullable UIImage *)imageWithIdentifier:(NSString *)identifier;
  53. @end
  54. /**
  55. The `ImageRequestCache` protocol extends the `ImageCache` protocol by adding methods for adding, removing and fetching images from a cache given an `NSURLRequest` and additional identifier.
  56. */
  57. @protocol AFImageRequestCache <AFImageCache>
  58. /**
  59. Asks if the image should be cached using an identifier created from the request and additional identifier.
  60. @param image The image to be cached.
  61. @param request The unique URL request identifing the image asset.
  62. @param identifier The additional identifier to apply to the URL request to identify the image.
  63. @return A BOOL indicating whether or not the image should be added to the cache. YES will cache, NO will prevent caching.
  64. */
  65. - (BOOL)shouldCacheImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
  66. /**
  67. Adds the image to the cache using an identifier created from the request and additional identifier.
  68. @param image The image to cache.
  69. @param request The unique URL request identifing the image asset.
  70. @param identifier The additional identifier to apply to the URL request to identify the image.
  71. */
  72. - (void)addImage:(UIImage *)image forRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
  73. /**
  74. Removes the image from the cache using an identifier created from the request and additional identifier.
  75. @param request The unique URL request identifing the image asset.
  76. @param identifier The additional identifier to apply to the URL request to identify the image.
  77. @return A BOOL indicating whether or not all images were removed from the cache.
  78. */
  79. - (BOOL)removeImageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
  80. /**
  81. Returns the image from the cache associated with an identifier created from the request and additional identifier.
  82. @param request The unique URL request identifing the image asset.
  83. @param identifier The additional identifier to apply to the URL request to identify the image.
  84. @return An image for the matching request and identifier, or nil.
  85. */
  86. - (nullable UIImage *)imageforRequest:(NSURLRequest *)request withAdditionalIdentifier:(nullable NSString *)identifier;
  87. @end
  88. /**
  89. The `AutoPurgingImageCache` in an in-memory image cache used to store images up to a given memory capacity. When the memory capacity is reached, the image cache is sorted by last access date, then the oldest image is continuously purged until the preferred memory usage after purge is met. Each time an image is accessed through the cache, the internal access date of the image is updated.
  90. */
  91. @interface AFAutoPurgingImageCache : NSObject <AFImageRequestCache>
  92. /**
  93. The total memory capacity of the cache in bytes.
  94. */
  95. @property (nonatomic, assign) UInt64 memoryCapacity;
  96. /**
  97. The preferred memory usage after purge in bytes. During a purge, images will be purged until the memory capacity drops below this limit.
  98. */
  99. @property (nonatomic, assign) UInt64 preferredMemoryUsageAfterPurge;
  100. /**
  101. The current total memory usage in bytes of all images stored within the cache.
  102. */
  103. @property (nonatomic, assign, readonly) UInt64 memoryUsage;
  104. /**
  105. Initialies the `AutoPurgingImageCache` instance with default values for memory capacity and preferred memory usage after purge limit. `memoryCapcity` defaults to `100 MB`. `preferredMemoryUsageAfterPurge` defaults to `60 MB`.
  106. @return The new `AutoPurgingImageCache` instance.
  107. */
  108. - (instancetype)init;
  109. /**
  110. Initialies the `AutoPurgingImageCache` instance with the given memory capacity and preferred memory usage
  111. after purge limit.
  112. @param memoryCapacity The total memory capacity of the cache in bytes.
  113. @param preferredMemoryCapacity The preferred memory usage after purge in bytes.
  114. @return The new `AutoPurgingImageCache` instance.
  115. */
  116. - (instancetype)initWithMemoryCapacity:(UInt64)memoryCapacity preferredMemoryCapacity:(UInt64)preferredMemoryCapacity;
  117. @end
  118. NS_ASSUME_NONNULL_END
  119. #endif