Merge pull request #498 from donholly/master

Added and exposed some methods that allow to check if an image exists on...
This commit is contained in:
Olivier Poitrey 2013-09-20 11:03:18 -07:00
commit 9820cafe9b
4 changed files with 35 additions and 1 deletions

View File

@ -159,4 +159,9 @@ typedef enum SDImageCacheType SDImageCacheType;
*/ */
- (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, unsigned long long totalSize))completionBlock; - (void)calculateSizeWithCompletionBlock:(void (^)(NSUInteger fileCount, unsigned long long totalSize))completionBlock;
/**
* Check if image exists in cache already
*/
- (BOOL)diskImageExistsWithKey:(NSString *)key;
@end @end

View File

@ -25,7 +25,9 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
@end @end
@implementation SDImageCache @implementation SDImageCache {
NSFileManager *_fileManager;
}
+ (SDImageCache *)sharedImageCache + (SDImageCache *)sharedImageCache
{ {
@ -60,6 +62,11 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
_diskCachePath = [paths[0] stringByAppendingPathComponent:fullNamespace]; _diskCachePath = [paths[0] stringByAppendingPathComponent:fullNamespace];
dispatch_sync(_ioQueue, ^
{
_fileManager = NSFileManager.new;
});
#if TARGET_OS_IPHONE #if TARGET_OS_IPHONE
// Subscribe to app events // Subscribe to app events
[[NSNotificationCenter defaultCenter] addObserver:self [[NSNotificationCenter defaultCenter] addObserver:self
@ -184,6 +191,17 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
[self storeImage:image imageData:nil forKey:key toDisk:toDisk]; [self storeImage:image imageData:nil forKey:key toDisk:toDisk];
} }
- (BOOL)diskImageExistsWithKey:(NSString *)key
{
__block BOOL exists = NO;
dispatch_sync(_ioQueue, ^
{
exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];
});
return exists;
}
- (UIImage *)imageFromMemoryCacheForKey:(NSString *)key - (UIImage *)imageFromMemoryCacheForKey:(NSString *)key
{ {
return [self.memCache objectForKey:key]; return [self.memCache objectForKey:key];

View File

@ -171,4 +171,9 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
*/ */
- (BOOL)isRunning; - (BOOL)isRunning;
/**
* Check if image has already been cached
*/
- (BOOL)diskImageExistsForURL:(NSURL *)url;
@end @end

View File

@ -66,6 +66,12 @@
} }
} }
- (BOOL)diskImageExistsForURL:(NSURL *)url
{
NSString *key = [self cacheKeyForURL:url];
return [self.imageCache diskImageExistsWithKey:key];
}
- (id<SDWebImageOperation>)downloadWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedWithFinishedBlock)completedBlock - (id<SDWebImageOperation>)downloadWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedWithFinishedBlock)completedBlock
{ {
// Invoking this method without a completedBlock is pointless // Invoking this method without a completedBlock is pointless