Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add synchronous disk-cache loading method.

  • Loading branch information...
commit 17cc72d5ab62615ae8d1a6fcd8198d5b6e28a2fc 1 parent de485a8
@inamiy inamiy authored
Showing with 28 additions and 0 deletions.
  1. +7 −0 SDWebImage/SDImageCache.h
  2. +21 −0 SDWebImage/SDImageCache.m
View
7 SDWebImage/SDImageCache.h
@@ -95,6 +95,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
*
* @param key The unique image cache key
View
21 SDWebImage/SDImageCache.m
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.