diff --git a/SDWebImageManager.h b/SDWebImageManager.h index f5578756..17c5d71c 100644 --- a/SDWebImageManager.h +++ b/SDWebImageManager.h @@ -11,6 +11,13 @@ #import "SDWebImageManagerDelegate.h" #import "SDImageCacheDelegate.h" +typedef enum +{ + SDWebImageRetryFailed = 1, + SDWebImageLowPriority, + SDWebImageCacheMemoryOnly +} SDWebImageOptions; + @interface SDWebImageManager : NSObject { NSMutableArray *downloadDelegates; @@ -23,8 +30,9 @@ + (id)sharedManager; - (UIImage *)imageWithURL:(NSURL *)url; - (void)downloadWithURL:(NSURL *)url delegate:(id)delegate; -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate retryFailed:(BOOL)retryFailed; -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority; +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options; +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate retryFailed:(BOOL)retryFailed __attribute__ ((deprecated)); // use options:SDWebImageRetryFailed instead +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority __attribute__ ((deprecated)); // use options:SDWebImageRetryFailed|SDWebImageLowPriority instead - (void)cancelForDelegate:(id)delegate; @end diff --git a/SDWebImageManager.m b/SDWebImageManager.m index b9571c60..675387a3 100644 --- a/SDWebImageManager.m +++ b/SDWebImageManager.m @@ -56,23 +56,32 @@ static SDWebImageManager *instance; return [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]]; } -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate -{ - [self downloadWithURL: url delegate:delegate retryFailed:NO]; -} - +/** + * @deprecated + */ - (void)downloadWithURL:(NSURL *)url delegate:(id)delegate retryFailed:(BOOL)retryFailed { - [self downloadWithURL:url delegate:delegate retryFailed:retryFailed lowPriority:NO]; + [self downloadWithURL:url delegate:delegate options:(retryFailed ? SDWebImageRetryFailed : 0)]; } +/** + * @deprecated + */ - (void)downloadWithURL:(NSURL *)url delegate:(id)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority { - if (!url || !delegate || (!retryFailed && [failedURLs containsObject:url])) - { - return; - } + SDWebImageOptions options = 0; + if (retryFailed) options |= SDWebImageRetryFailed; + if (lowPriority) options |= SDWebImageLowPriority; + [self downloadWithURL:url delegate:delegate options:options]; +} +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate +{ + [self downloadWithURL:url delegate:delegate options:0]; +} + +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options +{ // Very common mistake is to send the URL using NSString object instead of NSURL. For some strange reason, XCode won't // throw any warning for this type mismatch. Here we failsafe this error by allowing URLs to be passed as NSString. if ([url isKindOfClass:NSString.class]) @@ -80,9 +89,14 @@ static SDWebImageManager *instance; url = [NSURL URLWithString:(NSString *)url]; } + if (!url || !delegate || (!(options & SDWebImageRetryFailed) && [failedURLs containsObject:url])) + { + return; + } + // Check the on-disk cache async so we don't block the main thread [cacheDelegates addObject:delegate]; - NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithBool:lowPriority], @"low_priority", nil]; + NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithInt:options], @"options", nil]; [[SDImageCache sharedImageCache] queryDiskCacheForKey:[url absoluteString] delegate:self userInfo:info]; } @@ -139,7 +153,7 @@ static SDWebImageManager *instance; { NSURL *url = [info objectForKey:@"url"]; id delegate = [info objectForKey:@"delegate"]; - BOOL lowPriority = [[info objectForKey:@"low_priority"] boolValue]; + SDWebImageOptions options = [[info objectForKey:@"options"] intValue]; NSUInteger idx = [cacheDelegates indexOfObjectIdenticalTo:delegate]; if (idx == NSNotFound) @@ -155,14 +169,14 @@ static SDWebImageManager *instance; if (!downloader) { - downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self userInfo:nil lowPriority:lowPriority]; + downloader = [SDWebImageDownloader downloaderWithURL:url delegate:self userInfo:info lowPriority:(options & SDWebImageLowPriority)]; [downloaderForURL setObject:downloader forKey:url]; } - - // If we get a normal priority request, make sure to change type since downloader is shared - if (!lowPriority && downloader.lowPriority) + else { - downloader.lowPriority = NO; + // Reuse shared downloader + downloader.userInfo = info; + downloader.lowPriority = (options & SDWebImageLowPriority); } [downloadDelegates addObject:delegate]; @@ -174,6 +188,7 @@ static SDWebImageManager *instance; - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(UIImage *)image { [downloader retain]; + SDWebImageOptions options = [[downloader.userInfo objectForKey:@"options"] intValue]; // Notify all the downloadDelegates with this downloader for (NSInteger idx = (NSInteger)[downloaders count] - 1; idx >= 0; idx--) @@ -210,11 +225,12 @@ static SDWebImageManager *instance; [[SDImageCache sharedImageCache] storeImage:image imageData:downloader.imageData forKey:[downloader.url absoluteString] - toDisk:YES]; + toDisk:!(options & SDWebImageCacheMemoryOnly)]; } - else + else if (!(options & SDWebImageRetryFailed)) { // The image can't be downloaded from this URL, mark the URL as failed so we won't try and fail again and again + // (do this only if SDWebImageRetryFailed isn't activated) [failedURLs addObject:downloader.url]; } diff --git a/UIImageView+WebCache.h b/UIImageView+WebCache.h index 9d93a8ef..5d76a182 100644 --- a/UIImageView+WebCache.h +++ b/UIImageView+WebCache.h @@ -8,11 +8,13 @@ #import "SDWebImageCompat.h" #import "SDWebImageManagerDelegate.h" +#import "SDWebImageManager.h" @interface UIImageView (WebCache) - (void)setImageWithURL:(NSURL *)url; - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options; - (void)cancelCurrentImageLoad; @end diff --git a/UIImageView+WebCache.m b/UIImageView+WebCache.m index 7b70dd1d..71643456 100644 --- a/UIImageView+WebCache.m +++ b/UIImageView+WebCache.m @@ -7,7 +7,6 @@ */ #import "UIImageView+WebCache.h" -#import "SDWebImageManager.h" @implementation UIImageView (WebCache) @@ -17,6 +16,11 @@ } - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder +{ + [self setImageWithURL:url placeholderImage:placeholder options:0]; +} + +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options { SDWebImageManager *manager = [SDWebImageManager sharedManager]; @@ -27,7 +31,7 @@ if (url) { - [manager downloadWithURL:url delegate:self]; + [manager downloadWithURL:url delegate:self options:options]; } }