...
|
...
|
@@ -26,13 +26,12 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
@property (strong, nonatomic, nonnull) NSString *diskCachePath;
|
|
|
@property (strong, nonatomic, nullable) NSMutableArray<NSString *> *customPaths;
|
|
|
@property (strong, nonatomic, nullable) dispatch_queue_t ioQueue;
|
|
|
@property (strong, nonatomic, nonnull) NSFileManager *fileManager;
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
@implementation SDImageCache {
|
|
|
NSFileManager *_fileManager;
|
|
|
}
|
|
|
@implementation SDImageCache
|
|
|
|
|
|
#pragma mark - Singleton, init, dealloc
|
|
|
|
...
|
...
|
@@ -224,8 +223,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (![_fileManager fileExistsAtPath:_diskCachePath]) {
|
|
|
[_fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
|
|
|
if (![self.fileManager fileExistsAtPath:_diskCachePath]) {
|
|
|
[self.fileManager createDirectoryAtPath:_diskCachePath withIntermediateDirectories:YES attributes:nil error:NULL];
|
|
|
}
|
|
|
|
|
|
// get cache Path for image key
|
...
|
...
|
@@ -271,12 +270,12 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
if (!key) {
|
|
|
return NO;
|
|
|
}
|
|
|
BOOL exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];
|
|
|
BOOL exists = [self.fileManager fileExistsAtPath:[self defaultCachePathForKey:key]];
|
|
|
|
|
|
// fallback because of https://github.com/rs/SDWebImage/pull/976 that added the extension to the disk file name
|
|
|
// checking the key with and without the extension
|
|
|
if (!exists) {
|
|
|
exists = [_fileManager fileExistsAtPath:[self defaultCachePathForKey:key].stringByDeletingPathExtension];
|
|
|
exists = [self.fileManager fileExistsAtPath:[self defaultCachePathForKey:key].stringByDeletingPathExtension];
|
|
|
}
|
|
|
|
|
|
return exists;
|
...
|
...
|
@@ -447,7 +446,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
|
|
|
if (fromDisk) {
|
|
|
dispatch_async(self.ioQueue, ^{
|
|
|
[_fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
|
|
|
[self.fileManager removeItemAtPath:[self defaultCachePathForKey:key] error:nil];
|
|
|
|
|
|
if (completion) {
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
...
|
...
|
@@ -487,8 +486,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
|
|
|
- (void)clearDiskOnCompletion:(nullable SDWebImageNoParamsBlock)completion {
|
|
|
dispatch_async(self.ioQueue, ^{
|
|
|
[_fileManager removeItemAtPath:self.diskCachePath error:nil];
|
|
|
[_fileManager createDirectoryAtPath:self.diskCachePath
|
|
|
[self.fileManager removeItemAtPath:self.diskCachePath error:nil];
|
|
|
[self.fileManager createDirectoryAtPath:self.diskCachePath
|
|
|
withIntermediateDirectories:YES
|
|
|
attributes:nil
|
|
|
error:NULL];
|
...
|
...
|
@@ -511,7 +510,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
|
|
|
|
|
|
// This enumerator prefetches useful properties for our cache files.
|
|
|
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
|
|
|
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
|
|
|
includingPropertiesForKeys:resourceKeys
|
|
|
options:NSDirectoryEnumerationSkipsHiddenFiles
|
|
|
errorHandler:NULL];
|
...
|
...
|
@@ -548,7 +547,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
}
|
|
|
|
|
|
for (NSURL *fileURL in urlsToDelete) {
|
|
|
[_fileManager removeItemAtURL:fileURL error:nil];
|
|
|
[self.fileManager removeItemAtURL:fileURL error:nil];
|
|
|
}
|
|
|
|
|
|
// If our remaining disk cache exceeds a configured maximum size, perform a second
|
...
|
...
|
@@ -565,7 +564,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
|
|
|
// Delete files until we fall below our desired cache size.
|
|
|
for (NSURL *fileURL in sortedFiles) {
|
|
|
if ([_fileManager removeItemAtURL:fileURL error:nil]) {
|
|
|
if ([self.fileManager removeItemAtURL:fileURL error:nil]) {
|
|
|
NSDictionary<NSString *, id> *resourceValues = cacheFiles[fileURL];
|
|
|
NSNumber *totalAllocatedSize = resourceValues[NSURLTotalFileAllocatedSizeKey];
|
|
|
currentCacheSize -= totalAllocatedSize.unsignedIntegerValue;
|
...
|
...
|
@@ -611,10 +610,10 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
- (NSUInteger)getSize {
|
|
|
__block NSUInteger size = 0;
|
|
|
dispatch_sync(self.ioQueue, ^{
|
|
|
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
|
|
|
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtPath:self.diskCachePath];
|
|
|
for (NSString *fileName in fileEnumerator) {
|
|
|
NSString *filePath = [self.diskCachePath stringByAppendingPathComponent:fileName];
|
|
|
NSDictionary<NSString *, id> *attrs = [_fileManager attributesOfItemAtPath:filePath error:nil];
|
|
|
NSDictionary<NSString *, id> *attrs = [self.fileManager attributesOfItemAtPath:filePath error:nil];
|
|
|
size += [attrs fileSize];
|
|
|
}
|
|
|
});
|
...
|
...
|
@@ -624,7 +623,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
- (NSUInteger)getDiskCount {
|
|
|
__block NSUInteger count = 0;
|
|
|
dispatch_sync(self.ioQueue, ^{
|
|
|
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtPath:self.diskCachePath];
|
|
|
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtPath:self.diskCachePath];
|
|
|
count = fileEnumerator.allObjects.count;
|
|
|
});
|
|
|
return count;
|
...
|
...
|
@@ -637,7 +636,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { |
|
|
NSUInteger fileCount = 0;
|
|
|
NSUInteger totalSize = 0;
|
|
|
|
|
|
NSDirectoryEnumerator *fileEnumerator = [_fileManager enumeratorAtURL:diskCacheURL
|
|
|
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
|
|
|
includingPropertiesForKeys:@[NSFileSize]
|
|
|
options:NSDirectoryEnumerationSkipsHiddenFiles
|
|
|
errorHandler:NULL];
|
...
|
...
|
|