feat: store image data

This commit is contained in:
Inso 2022-03-28 12:41:22 +08:00
parent 2e63d0061d
commit 6a2eaf707e
2 changed files with 41 additions and 1 deletions

View File

@ -179,6 +179,31 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock;
/**
* Asynchronously store an image data into memory and disk cache at the given key.
*
* @param imageData The image data to store
* @param key The unique image cache key, usually it's image absolute URL
* @param completionBlock A block executed after the operation is finished
*/
- (void)storeImageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
completion:(nullable SDWebImageNoParamsBlock)completionBlock;
/**
* Asynchronously store an image data into memory and disk cache at the given key.
*
* @param imageData The image data to store
* @param key The unique image cache key, usually it's image absolute URL
* @param toDisk Store the image to disk cache if YES. If NO, the completion block is called synchronously
* @param completionBlock A block executed after the operation is finished
* @note If no image data is provided and encode to disk, we will try to detect the image format (using either `sd_imageFormat` or `SDAnimatedImage` protocol method) and animation status, to choose the best matched format, including GIF, JPEG or PNG.
*/
- (void)storeImageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock;
/**
* Asynchronously store an image into memory and disk cache at the given key.
*

View File

@ -168,6 +168,17 @@ static NSString * _defaultDiskCacheDirectory;
[self storeImage:image imageData:nil forKey:key toDisk:toDisk completion:completionBlock];
}
- (void)storeImageData:(NSData *)imageData
forKey:(NSString *)key
completion:(SDWebImageNoParamsBlock)completionBlock {
[self storeImageData:imageData forKey:key toDisk:YES completion:completionBlock];
}
- (void)storeImageData:(NSData *)imageData forKey:(NSString *)key toDisk:(BOOL)toDisk completion:(SDWebImageNoParamsBlock)completionBlock {
[self storeImage:nil imageData:imageData forKey:key toDisk:toDisk completion:completionBlock];
}
- (void)storeImage:(nullable UIImage *)image
imageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
@ -182,7 +193,7 @@ static NSString * _defaultDiskCacheDirectory;
toMemory:(BOOL)toMemory
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
if (!image || !key) {
if ((!image && !imageData) || !key) {
if (completionBlock) {
completionBlock();
}
@ -191,6 +202,10 @@ static NSString * _defaultDiskCacheDirectory;
// if memory cache is enabled
if (toMemory && self.config.shouldCacheImagesInMemory) {
NSUInteger cost = image.sd_memoryCost;
if (!image && imageData) {
cost = imageData.length;
image = [UIImage imageWithData:imageData];
}
[self.memoryCache setObject:image forKey:key cost:cost];
}