diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index 9b07f47c..ce259897 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -178,6 +178,14 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot */ - (BOOL)diskImageDataExistsWithKey:(nullable NSString *)key; +/** + * Query the image data for the given key synchronously. + * + * @param key The unique key used to store the wanted image + * @return The image data for the given key, or nil if not found. + */ +- (nullable NSData *)diskImageDataForKey:(nullable NSString *)key; + /** * Operation that queries the cache asynchronously and call the completion when done. * @@ -203,6 +211,7 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot * Query the memory cache synchronously. * * @param key The unique key used to store the image + * @return The image for the given key, or nil if not found. */ - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key; @@ -210,6 +219,7 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot * Query the disk cache synchronously. * * @param key The unique key used to store the image + * @return The image for the given key, or nil if not found. */ - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key; @@ -217,6 +227,7 @@ typedef void(^SDWebImageCalculateSizeBlock)(NSUInteger fileCount, NSUInteger tot * Query the cache (memory and or disk) synchronously after checking the memory cache. * * @param key The unique key used to store the image + * @return The image for the given key, or nil if not found. */ - (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key; diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index 33592b14..8e116d18 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -399,6 +399,18 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { return exists; } +- (nullable NSData *)diskImageDataForKey:(nullable NSString *)key { + if (!key) { + return nil; + } + __block NSData *imageData = nil; + dispatch_sync(self.ioQueue, ^{ + imageData = [self diskImageDataBySearchingAllPathsForKey:key]; + }); + + return imageData; +} + - (nullable UIImage *)imageFromMemoryCacheForKey:(nullable NSString *)key { return [self.memCache objectForKey:key]; } @@ -459,7 +471,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } - (nullable UIImage *)diskImageForKey:(nullable NSString *)key { - NSData *data = [self diskImageDataBySearchingAllPathsForKey:key]; + NSData *data = [self diskImageDataForKey:key]; return [self diskImageForKey:key data:data]; }