Skip to content

Commit 30f6726

Browse files
committed
Added option to use the cached image instead of the placeholder for UIImageView. Replaces #541. Fixes #599
1 parent a6f11b3 commit 30f6726

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

SDWebImage/UIImageView+WebCache.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,23 @@
145145
*/
146146
- (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
147147

148+
/**
149+
* Set the imageView `image` with an `url` and a optionaly placeholder image.
150+
*
151+
* The download is asynchronous and cached.
152+
*
153+
* @param url The url for the image.
154+
* @param placeholder The image to be set initially, until the image request finishes.
155+
* @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values.
156+
* @param progressBlock A block called while image is downloading
157+
* @param completedBlock A block called when operation has been completed. This block has no return value
158+
* and takes the requested UIImage as first parameter. In case of error the image parameter
159+
* is nil and the second parameter may contain an NSError. The third parameter is a Boolean
160+
* indicating if the image was retrived from the local cache of from the network.
161+
* The forth parameter is the original image url.
162+
*/
163+
- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock;
164+
148165
/**
149166
* Download an array of images and starts them in an animation loop
150167
*

SDWebImage/UIImageView+WebCache.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ - (void)sd_setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
7777
}
7878
}
7979

80+
- (void)sd_setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletionBlock)completedBlock {
81+
NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url];
82+
UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key];
83+
84+
[self sd_setImageWithURL:url placeholderImage:lastPreviousCachedImage ?: placeholder options:options progress:progressBlock completed:completedBlock];
85+
}
86+
8087
- (NSURL *)sd_imageURL {
8188
return objc_getAssociatedObject(self, &imageURLKey);
8289
}

0 commit comments

Comments
 (0)