Add an option to disable on disk caching (fix #16) + refactor other options (low prio and retry failed) to an options: parameter
This commit is contained in:
parent
7c06b3a314
commit
7d0e544c50
|
@ -11,6 +11,13 @@
|
||||||
#import "SDWebImageManagerDelegate.h"
|
#import "SDWebImageManagerDelegate.h"
|
||||||
#import "SDImageCacheDelegate.h"
|
#import "SDImageCacheDelegate.h"
|
||||||
|
|
||||||
|
typedef enum
|
||||||
|
{
|
||||||
|
SDWebImageRetryFailed = 1,
|
||||||
|
SDWebImageLowPriority,
|
||||||
|
SDWebImageCacheMemoryOnly
|
||||||
|
} SDWebImageOptions;
|
||||||
|
|
||||||
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>
|
@interface SDWebImageManager : NSObject <SDWebImageDownloaderDelegate, SDImageCacheDelegate>
|
||||||
{
|
{
|
||||||
NSMutableArray *downloadDelegates;
|
NSMutableArray *downloadDelegates;
|
||||||
|
@ -23,8 +30,9 @@
|
||||||
+ (id)sharedManager;
|
+ (id)sharedManager;
|
||||||
- (UIImage *)imageWithURL:(NSURL *)url;
|
- (UIImage *)imageWithURL:(NSURL *)url;
|
||||||
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate;
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate;
|
||||||
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed;
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate options:(SDWebImageOptions)options;
|
||||||
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority;
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed __attribute__ ((deprecated)); // use options:SDWebImageRetryFailed instead
|
||||||
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority __attribute__ ((deprecated)); // use options:SDWebImageRetryFailed|SDWebImageLowPriority instead
|
||||||
- (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate;
|
- (void)cancelForDelegate:(id<SDWebImageManagerDelegate>)delegate;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -56,23 +56,32 @@ static SDWebImageManager *instance;
|
||||||
return [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
|
return [[SDImageCache sharedImageCache] imageFromKey:[url absoluteString]];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate
|
/**
|
||||||
{
|
* @deprecated
|
||||||
[self downloadWithURL: url delegate:delegate retryFailed:NO];
|
*/
|
||||||
}
|
|
||||||
|
|
||||||
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)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<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed lowPriority:(BOOL)lowPriority
|
||||||
{
|
{
|
||||||
if (!url || !delegate || (!retryFailed && [failedURLs containsObject:url]))
|
SDWebImageOptions options = 0;
|
||||||
{
|
if (retryFailed) options |= SDWebImageRetryFailed;
|
||||||
return;
|
if (lowPriority) options |= SDWebImageLowPriority;
|
||||||
|
[self downloadWithURL:url delegate:delegate options:options];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate
|
||||||
|
{
|
||||||
|
[self downloadWithURL:url delegate:delegate options:0];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)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
|
// 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.
|
// 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])
|
if ([url isKindOfClass:NSString.class])
|
||||||
|
@ -80,9 +89,14 @@ static SDWebImageManager *instance;
|
||||||
url = [NSURL URLWithString:(NSString *)url];
|
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
|
// Check the on-disk cache async so we don't block the main thread
|
||||||
[cacheDelegates addObject:delegate];
|
[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];
|
[[SDImageCache sharedImageCache] queryDiskCacheForKey:[url absoluteString] delegate:self userInfo:info];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -139,7 +153,7 @@ static SDWebImageManager *instance;
|
||||||
{
|
{
|
||||||
NSURL *url = [info objectForKey:@"url"];
|
NSURL *url = [info objectForKey:@"url"];
|
||||||
id<SDWebImageManagerDelegate> delegate = [info objectForKey:@"delegate"];
|
id<SDWebImageManagerDelegate> delegate = [info objectForKey:@"delegate"];
|
||||||
BOOL lowPriority = [[info objectForKey:@"low_priority"] boolValue];
|
SDWebImageOptions options = [[info objectForKey:@"options"] intValue];
|
||||||
|
|
||||||
NSUInteger idx = [cacheDelegates indexOfObjectIdenticalTo:delegate];
|
NSUInteger idx = [cacheDelegates indexOfObjectIdenticalTo:delegate];
|
||||||
if (idx == NSNotFound)
|
if (idx == NSNotFound)
|
||||||
|
@ -155,14 +169,14 @@ static SDWebImageManager *instance;
|
||||||
|
|
||||||
if (!downloader)
|
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];
|
[downloaderForURL setObject:downloader forKey:url];
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// If we get a normal priority request, make sure to change type since downloader is shared
|
|
||||||
if (!lowPriority && downloader.lowPriority)
|
|
||||||
{
|
{
|
||||||
downloader.lowPriority = NO;
|
// Reuse shared downloader
|
||||||
|
downloader.userInfo = info;
|
||||||
|
downloader.lowPriority = (options & SDWebImageLowPriority);
|
||||||
}
|
}
|
||||||
|
|
||||||
[downloadDelegates addObject:delegate];
|
[downloadDelegates addObject:delegate];
|
||||||
|
@ -174,6 +188,7 @@ static SDWebImageManager *instance;
|
||||||
- (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(UIImage *)image
|
- (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(UIImage *)image
|
||||||
{
|
{
|
||||||
[downloader retain];
|
[downloader retain];
|
||||||
|
SDWebImageOptions options = [[downloader.userInfo objectForKey:@"options"] intValue];
|
||||||
|
|
||||||
// Notify all the downloadDelegates with this downloader
|
// Notify all the downloadDelegates with this downloader
|
||||||
for (NSInteger idx = (NSInteger)[downloaders count] - 1; idx >= 0; idx--)
|
for (NSInteger idx = (NSInteger)[downloaders count] - 1; idx >= 0; idx--)
|
||||||
|
@ -210,11 +225,12 @@ static SDWebImageManager *instance;
|
||||||
[[SDImageCache sharedImageCache] storeImage:image
|
[[SDImageCache sharedImageCache] storeImage:image
|
||||||
imageData:downloader.imageData
|
imageData:downloader.imageData
|
||||||
forKey:[downloader.url absoluteString]
|
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
|
// 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];
|
[failedURLs addObject:downloader.url];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,11 +8,13 @@
|
||||||
|
|
||||||
#import "SDWebImageCompat.h"
|
#import "SDWebImageCompat.h"
|
||||||
#import "SDWebImageManagerDelegate.h"
|
#import "SDWebImageManagerDelegate.h"
|
||||||
|
#import "SDWebImageManager.h"
|
||||||
|
|
||||||
@interface UIImageView (WebCache) <SDWebImageManagerDelegate>
|
@interface UIImageView (WebCache) <SDWebImageManagerDelegate>
|
||||||
|
|
||||||
- (void)setImageWithURL:(NSURL *)url;
|
- (void)setImageWithURL:(NSURL *)url;
|
||||||
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
|
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;
|
||||||
|
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;
|
||||||
- (void)cancelCurrentImageLoad;
|
- (void)cancelCurrentImageLoad;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#import "UIImageView+WebCache.h"
|
#import "UIImageView+WebCache.h"
|
||||||
#import "SDWebImageManager.h"
|
|
||||||
|
|
||||||
@implementation UIImageView (WebCache)
|
@implementation UIImageView (WebCache)
|
||||||
|
|
||||||
|
@ -17,6 +16,11 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
|
- (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];
|
SDWebImageManager *manager = [SDWebImageManager sharedManager];
|
||||||
|
|
||||||
|
@ -27,7 +31,7 @@
|
||||||
|
|
||||||
if (url)
|
if (url)
|
||||||
{
|
{
|
||||||
[manager downloadWithURL:url delegate:self];
|
[manager downloadWithURL:url delegate:self options:options];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue