Overload -setImageWithURL to delay setting of placeholder image until after loading

This commit is contained in:
Bill Burgess 2014-04-28 10:07:43 -05:00
parent 8fe72023dc
commit 12715da31b
2 changed files with 33 additions and 4 deletions

View File

@ -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
*

View File

@ -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 <SDWebImageOperation> 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);