Skip to content
This repository

Add synchronous disk-cache loading method. #297

Merged
merged 1 commit into from about 1 year ago

2 participants

Yasuhiro Inami Olivier Poitrey
Yasuhiro Inami

Hi, there are cases people just want to load images synchronously without using blocks,
so I added -imageFromDiskCacheForKey: method instead of using asynchronous -queryDiskCacheForKey:done:.

It'lll be glad if you can merge this commit to your master branch :)

Olivier Poitrey rs merged commit a8a3983 into from February 13, 2013
Olivier Poitrey rs closed this February 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 13, 2013
Yasuhiro Inami Add synchronous disk-cache loading method. 17cc72d
This page is out of date. Refresh to see the latest.
7  SDWebImage/SDImageCache.h
@@ -95,6 +95,13 @@ typedef enum SDImageCacheType SDImageCacheType;
95 95
 - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key;
96 96
 
97 97
 /**
  98
+ * Query the disk cache synchronousely.
  99
+ *
  100
+ * @param key The unique key used to store the wanted image
  101
+ */
  102
+- (UIImage *)imageFromDiskCacheForKey:(NSString *)key;
  103
+
  104
+/**
98 105
  * Remove the image from memory and disk cache synchronousely
99 106
  *
100 107
  * @param key The unique image cache key
21  SDWebImage/SDImageCache.m
@@ -155,6 +155,27 @@ - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key
155 155
     return [self.memCache objectForKey:key];
156 156
 }
157 157
 
  158
+- (UIImage *)imageFromDiskCacheForKey:(NSString *)key
  159
+{
  160
+    // First check the in-memory cache...
  161
+    UIImage *image = [self imageFromMemoryCacheForKey:key];
  162
+    if (image)
  163
+    {
  164
+        return image;
  165
+    }
  166
+    
  167
+    // Second check the disk cache...
  168
+    UIImage *diskImage = [UIImage decodedImageWithImage:SDScaledImageForPath(key, [NSData dataWithContentsOfFile:[self cachePathForKey:key]])];
  169
+    
  170
+    if (diskImage)
  171
+    {
  172
+        CGFloat cost = diskImage.size.height * diskImage.size.width * diskImage.scale;
  173
+        [self.memCache setObject:diskImage forKey:key cost:cost];
  174
+    }
  175
+    
  176
+    return diskImage;
  177
+}
  178
+
158 179
 - (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock
159 180
 {
160 181
     if (!doneBlock) return;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.