diff --git a/SDWebImage/Core/SDImageCache.h b/SDWebImage/Core/SDImageCache.h index ad3afd53..e8b2ee73 100644 --- a/SDWebImage/Core/SDImageCache.h +++ b/SDWebImage/Core/SDImageCache.h @@ -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 diff --git a/SDWebImage/Core/SDImageCache.m b/SDWebImage/Core/SDImageCache.m index 635908b3..a083b6ac 100644 --- a/SDWebImage/Core/SDImageCache.m +++ b/SDWebImage/Core/SDImageCache.m @@ -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