From a2eab4f34552dc738c3cbf9308ed0144c327fe22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jer=C3=B4me=20Morissard?= Date: Thu, 31 Oct 2013 18:18:22 +0100 Subject: [PATCH] [SDWebImage] Try to use an the previous cached image for placeholder --- SDWebImage/SDWebImageManager.h | 5 +++++ SDWebImage/UIImageView+WebCache.h | 13 ++++++++++++- SDWebImage/UIImageView+WebCache.m | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index b54af25e9..4c27eaadb 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -182,4 +182,9 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; */ - (BOOL)diskImageExistsForURL:(NSURL *)url; +/** + * Key for URL + */ +- (NSString *)cacheKeyForURL:(NSURL *)url; + @end diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index 952a1e9df..cd9b5450d 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -54,10 +54,21 @@ */ - (void)setImageWithURL:(NSURL *)url; +/** + * Set the imageView `image` with an `url` and a optionaly placeholder image. + * + * The download is asynchronous and cached. + * + * @param url The url for the image. + * @param placeholder The image to be set initially, until the image request finishes. + * @see setImageWithPreviousCachedImageWithURL:andPlaceholderImage: + */ +- (void)setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder; + /** * Set the imageView `image` with an `url` and a placeholder. * - * The downloand is asynchronous and cached. + * The download is asynchronous and cached. * * @param url The url for the image. * @param placeholder The image to be set initially, until the image request finishes. diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 7df6af87c..99100ee30 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -8,12 +8,25 @@ #import "UIImageView+WebCache.h" #import "objc/runtime.h" +#import "SDWebImageManager.h" static char operationKey; static char operationArrayKey; @implementation UIImageView (WebCache) +- (void)setImageWithPreviousCachedImageWithURL:(NSURL *)url andPlaceholderImage:(UIImage *)placeholder +{ + NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:url]; + UIImage *lastPreviousCachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:key]; + if (nil == lastPreviousCachedImage) { + [self setImageWithURL:url placeholderImage:placeholder options:0 progress:nil completed:nil]; + } + else { + [self setImageWithURL:url placeholderImage:lastPreviousCachedImage options:0 progress:nil completed:nil]; + } +} + - (void)setImageWithURL:(NSURL *)url { [self setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];