diff --git a/SDWebImage/Core/SDImageCache.h b/SDWebImage/Core/SDImageCache.h index ad3afd53..926fe78d 100644 --- a/SDWebImage/Core/SDImageCache.h +++ b/SDWebImage/Core/SDImageCache.h @@ -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. * diff --git a/SDWebImage/Core/SDImageCache.m b/SDWebImage/Core/SDImageCache.m index 635908b3..367ff5c0 100644 --- a/SDWebImage/Core/SDImageCache.m +++ b/SDWebImage/Core/SDImageCache.m @@ -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]; }