From 12715da31b591ae888c05f5307e6a8997af03e2d Mon Sep 17 00:00:00 2001 From: Bill Burgess Date: Mon, 28 Apr 2014 10:07:43 -0500 Subject: [PATCH 1/4] Overload -setImageWithURL to delay setting of placeholder image until after loading --- SDWebImage/UIImageView+WebCache.h | 20 +++++++++++++++++++- SDWebImage/UIImageView+WebCache.m | 17 ++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index 46f1f0b0..c337088e 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -120,7 +120,7 @@ /** * Set the imageView `image` with an `url`, placeholder and custom options. * - * 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. @@ -133,6 +133,24 @@ */ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; +/** + * Set the imageView 'image' with an 'url', placeholder and custom options. + * + * 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. + * @param delayPlaceholder The value that determines if the placeholder is loaded while the image is fetched, + * or after the image fails to load. + * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. + * @param progressBlock A block called while image is downloading + * @param completedBlock A block called when operation has been completed. This block as no return value + * and takes the requested UIImage as first parameter. In case of error the image parameter + * is nil and the second parameter may contain an NSError. The third parameter is a Boolean + * indicating if the image was retrived from the local cache of from the network. + */ +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder delayPlaceholderLoad:(BOOL)delayPlaceholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; + /** * Download an array of images and starts them in an animation loop * diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index ed65a0ed..d7f7fa46 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -39,10 +39,16 @@ static char operationArrayKey; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { + [self setImageWithURL:url placeholderImage:placeholder delayPlaceholderLoad:NO options:options progress:progressBlock completed:completedBlock]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder delayPlaceholderLoad:(BOOL)delayPlaceholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; - - self.image = placeholder; - + + if (!delayPlaceholder) { + self.image = placeholder; + } + if (url) { __weak UIImageView *wself = self; id operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { @@ -52,6 +58,11 @@ static char operationArrayKey; if (image) { wself.image = image; [wself setNeedsLayout]; + } else { + if (delayPlaceholder) { + wself.image = placeholder; + [wself setNeedsLayout]; + } } if (completedBlock && finished) { completedBlock(image, error, cacheType); From f7ee9d337513fdb181543f9b7ad80e38f7fcb3ec Mon Sep 17 00:00:00 2001 From: Bill Burgess Date: Mon, 28 Apr 2014 12:30:27 -0500 Subject: [PATCH 2/4] Moved the delay placeholder flag to SDWebImageOptions --- SDWebImage/SDWebImageManager.h | 8 +++++++- SDWebImage/UIImageView+WebCache.h | 18 ------------------ SDWebImage/UIImageView+WebCache.m | 8 ++------ 3 files changed, 9 insertions(+), 25 deletions(-) diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 45598c46..003d1ffa 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -68,7 +68,13 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { * the front of the queue and is loaded immediately instead of waiting for the current queue to be loaded (which * could take a while). */ - SDWebImageHighPriority = 1 << 8 + SDWebImageHighPriority = 1 << 8, + + /** + * By default, placeholder images are loaded while the image is loading. This flag will delay the loading + * of the placeholder image until after the image has finished loading. + */ + SDWebImageDelayPlaceholder = 1 << 9 }; typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType); diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index c337088e..cfdd6757 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -133,24 +133,6 @@ */ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; -/** - * Set the imageView 'image' with an 'url', placeholder and custom options. - * - * 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. - * @param delayPlaceholder The value that determines if the placeholder is loaded while the image is fetched, - * or after the image fails to load. - * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. - * @param progressBlock A block called while image is downloading - * @param completedBlock A block called when operation has been completed. This block as no return value - * and takes the requested UIImage as first parameter. In case of error the image parameter - * is nil and the second parameter may contain an NSError. The third parameter is a Boolean - * indicating if the image was retrived from the local cache of from the network. - */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder delayPlaceholderLoad:(BOOL)delayPlaceholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock; - /** * Download an array of images and starts them in an animation loop * diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index d7f7fa46..f425506b 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -39,13 +39,9 @@ static char operationArrayKey; } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { - [self setImageWithURL:url placeholderImage:placeholder delayPlaceholderLoad:NO options:options progress:progressBlock completed:completedBlock]; -} - -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder delayPlaceholderLoad:(BOOL)delayPlaceholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; - if (!delayPlaceholder) { + if (options != SDWebImageDelayPlaceholder) { self.image = placeholder; } @@ -59,7 +55,7 @@ static char operationArrayKey; wself.image = image; [wself setNeedsLayout]; } else { - if (delayPlaceholder) { + if (options == SDWebImageDelayPlaceholder) { wself.image = placeholder; [wself setNeedsLayout]; } From 7ca81d0e36612a7df27fcdace61aca157963cc83 Mon Sep 17 00:00:00 2001 From: Bill Burgess Date: Tue, 29 Apr 2014 10:13:28 -0500 Subject: [PATCH 3/4] Updated bitwise operator to correctly find option if more than 1 are included --- SDWebImage/UIImageView+WebCache.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index f425506b..2e2fb9d9 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -41,7 +41,7 @@ static char operationArrayKey; - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedBlock)completedBlock { [self cancelCurrentImageLoad]; - if (options != SDWebImageDelayPlaceholder) { + if (!(options & SDWebImageDelayPlaceholder)) { self.image = placeholder; } @@ -55,7 +55,7 @@ static char operationArrayKey; wself.image = image; [wself setNeedsLayout]; } else { - if (options == SDWebImageDelayPlaceholder) { + if (options & SDWebImageDelayPlaceholder) { wself.image = placeholder; [wself setNeedsLayout]; } From 9a45faac3f9169bd78fff9a63175eeec3c410657 Mon Sep 17 00:00:00 2001 From: Bill Burgess Date: Tue, 29 Apr 2014 10:27:54 -0500 Subject: [PATCH 4/4] Wrapped placeholder check in parens to work with bitwise --- SDWebImage/UIImageView+WebCache.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 2e2fb9d9..e0de97d0 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -55,7 +55,7 @@ static char operationArrayKey; wself.image = image; [wself setNeedsLayout]; } else { - if (options & SDWebImageDelayPlaceholder) { + if ((options & SDWebImageDelayPlaceholder)) { wself.image = placeholder; [wself setNeedsLayout]; }