Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add synchronous disk-cache loading method. #297

Merged
merged 1 commit into from Feb 13, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions SDWebImage/SDImageCache.h
Expand Up @@ -94,6 +94,13 @@ typedef enum SDImageCacheType SDImageCacheType;
*/
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key;

/**
* Query the disk cache synchronousely.
*
* @param key The unique key used to store the wanted image
*/
- (UIImage *)imageFromDiskCacheForKey:(NSString *)key;

/**
* Remove the image from memory and disk cache synchronousely
*
Expand Down
21 changes: 21 additions & 0 deletions SDWebImage/SDImageCache.m
Expand Up @@ -155,6 +155,27 @@ - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key
return [self.memCache objectForKey:key];
}

- (UIImage *)imageFromDiskCacheForKey:(NSString *)key
{
// First check the in-memory cache...
UIImage *image = [self imageFromMemoryCacheForKey:key];
if (image)
{
return image;
}

// Second check the disk cache...
UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])];

if (diskImage)
{
CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
[self.memCache setObject:diskImage forKey:key cost:cost];
}

return diskImage;
}

- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock
{
if (!doneBlock) return;
Expand Down