Add one sync API for SDImageCache to directly get the image data from disk instead of image instance

This commit is contained in:
DreamPiggy 2018-07-18 13:05:35 +08:00
parent 39a28791fd
commit 5e52585944
2 changed files with 24 additions and 1 deletions

View File

@ -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;

View File

@ -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];
}