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