Merge pull request #3336 from Insofan/feat/store_data

feat: store image data
This commit is contained in:
DreamPiggy 2022-06-20 17:18:42 +08:00 committed by GitHub
commit 9362505aeb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 5 deletions

View File

@ -179,6 +179,17 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock;
/**
* Asynchronously store an image data into 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 into memory and disk cache at the given key.
*
@ -198,7 +209,7 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
completion:(nullable SDWebImageNoParamsBlock)completionBlock;
/**
* Synchronously store image into memory cache at the given key.
* Synchronously store an image into memory cache at the given key.
*
* @param image The image to store
* @param key The unique image cache key, usually it's image absolute URL
@ -207,7 +218,7 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) {
forKey:(nullable NSString *)key;
/**
* Synchronously store image data into disk cache at the given key.
* Synchronously store an image data into 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

View File

@ -168,6 +168,12 @@ static NSString * _defaultDiskCacheDirectory;
[self storeImage:image imageData:nil forKey:key toDisk:toDisk completion:completionBlock];
}
- (void)storeImageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
[self storeImage:nil imageData:imageData forKey:key toDisk:YES completion:completionBlock];
}
- (void)storeImage:(nullable UIImage *)image
imageData:(nullable NSData *)imageData
forKey:(nullable NSString *)key
@ -182,14 +188,14 @@ static NSString * _defaultDiskCacheDirectory;
toMemory:(BOOL)toMemory
toDisk:(BOOL)toDisk
completion:(nullable SDWebImageNoParamsBlock)completionBlock {
if (!image || !key) {
if ((!image && !imageData) || !key) {
if (completionBlock) {
completionBlock();
}
return;
}
// if memory cache is enabled
if (toMemory && self.config.shouldCacheImagesInMemory) {
if (image && toMemory && self.config.shouldCacheImagesInMemory) {
NSUInteger cost = image.sd_memoryCost;
[self.memoryCache setObject:image forKey:key cost:cost];
}
@ -234,7 +240,7 @@ static NSString * _defaultDiskCacheDirectory;
}
- (void)_archivedDataWithImage:(UIImage *)image forKey:(NSString *)key {
if (!image) {
if (!image || !key) {
return;
}
// Check extended data