Merge branch 'master' of https://github.com/rs/SDWebImage into 5.x

# Conflicts:
#	SDWebImage/SDImageCache.m
#	SDWebImage/SDImageCacheConfig.h
#	SDWebImage/SDImageCacheConfig.m
This commit is contained in:
DreamPiggy 2018-07-12 19:33:18 +08:00
commit 0108475578
6 changed files with 73 additions and 5 deletions

View File

@ -164,6 +164,7 @@ community can help you solve it.
- [Bogdan Poplauschi](https://github.com/bpoplauschi)
- [Chester Liu](https://github.com/skyline75489)
- [DreamPiggy](https://github.com/dreampiggy)
- [Wu Zhong](https://github.com/zhongwuzw)
## Licenses

View File

@ -52,6 +52,13 @@
*/
@property (nonatomic, assign) BOOL sd_predrawingEnabled;
/**
* Cache control for associated FLAnimatedImage object for memory cache. When enabled, we will bind created FLAnimatedImage instance to UIImage, and store it into memory cache to avoid create this instance cause decoding performance. See `UIImage+FLAnimatedImage`.
* When enabled, this may consume more memory, if you are facing memory issue, disable it and let FLAnimatedImage been created just in time and dealloced as it not been used. However, when disabled, this may impact performance since we need query disk cache, create FLAnimatedImage and decoding even when the current GIF url is cached.
* Defatuls to YES;
*/
@property (nonatomic, assign) BOOL sd_cacheFLAnimatedImage;
/**
* Load the image at the given url (either from cache or download) and load it in this imageView. It works with both static and dynamic images
* The download is asynchronous and cached.

View File

@ -56,6 +56,19 @@
objc_setAssociatedObject(self, @selector(sd_predrawingEnabled), @(sd_predrawingEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)sd_cacheFLAnimatedImage {
BOOL cacheFLAnimatedImage = YES;
NSNumber *value = objc_getAssociatedObject(self, @selector(sd_cacheFLAnimatedImage));
if ([value isKindOfClass:[NSNumber class]]) {
cacheFLAnimatedImage = value.boolValue;
}
return cacheFLAnimatedImage;
}
- (void)setSd_cacheFLAnimatedImage:(BOOL)sd_cacheFLAnimatedImage {
objc_setAssociatedObject(self, @selector(sd_cacheFLAnimatedImage), @(sd_cacheFLAnimatedImage), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)sd_setImageWithURL:(nullable NSURL *)url {
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
}
@ -125,12 +138,14 @@
FLAnimatedImage *animatedImage;
// Compatibility in 4.x for lower version FLAnimatedImage.
if ([FLAnimatedImage respondsToSelector:@selector(initWithAnimatedGIFData:optimalFrameCacheSize:predrawingEnabled:)]) {
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData optimalFrameCacheSize:self.sd_optimalFrameCacheSize predrawingEnabled:self.sd_predrawingEnabled];
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData optimalFrameCacheSize:weakSelf.sd_optimalFrameCacheSize predrawingEnabled:weakSelf.sd_predrawingEnabled];
} else {
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
}
dispatch_async(dispatch_get_main_queue(), ^{
image.sd_FLAnimatedImage = animatedImage;
if (weakSelf.sd_cacheFLAnimatedImage) {
image.sd_FLAnimatedImage = animatedImage;
}
weakSelf.animatedImage = animatedImage;
weakSelf.image = nil;
if (group) {

View File

@ -111,7 +111,23 @@
- (void)removeExpiredData {
NSURL *diskCacheURL = [NSURL fileURLWithPath:self.diskCachePath isDirectory:YES];
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey];
// Compute content date key to be used for tests
NSURLResourceKey cacheContentDateKey = NSURLContentModificationDateKey;
switch (self.config.diskCacheExpireType) {
case SDImageCacheConfigExpireTypeAccessDate:
cacheContentDateKey = NSURLContentAccessDateKey;
break;
case SDImageCacheConfigExpireTypeModificationDate:
cacheContentDateKey = NSURLContentModificationDateKey;
break;
default:
break;
}
NSArray<NSString *> *resourceKeys = @[NSURLIsDirectoryKey, cacheContentDateKey, NSURLTotalFileAllocatedSizeKey];
// This enumerator prefetches useful properties for our cache files.
NSDirectoryEnumerator *fileEnumerator = [self.fileManager enumeratorAtURL:diskCacheURL
@ -138,8 +154,8 @@
}
// Remove files that are older than the expiration date;
NSDate *modificationDate = resourceValues[NSURLContentModificationDateKey];
if ([[modificationDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
NSDate *modifiedDate = resourceValues[cacheContentDateKey];
if ([[modifiedDate laterDate:expirationDate] isEqualToDate:expirationDate]) {
[urlsToDelete addObject:fileURL];
continue;
}

View File

@ -9,6 +9,17 @@
#import <Foundation/Foundation.h>
#import "SDWebImageCompat.h"
typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
/**
* When the image is accessed it will update this value
*/
SDImageCacheConfigExpireTypeAccessDate,
/**
* The image was obtained from the disk cache (Default)
*/
SDImageCacheConfigExpireTypeModificationDate
};
// This class conform to NSCopying, make sure to add the property in `copyWithZone:` as well.
@interface SDImageCacheConfig : NSObject <NSCopying>
@ -26,10 +37,18 @@
/**
* Whether or not to use memory cache
* @note When the memory cache is disabled, the weak memory cache will also be disabled.
* Defaults to YES.
*/
@property (assign, nonatomic) BOOL shouldCacheImagesInMemory;
/*
* The option to control weak memory cache for images. When enable, `SDImageCache`'s memory cache will use a weak maptable to store the image at the same time when it stored to memory, and get removed at the same time.
* However when memory warning is triggered, since the weak maptable does not hold a strong reference to image instacnce, even when the memory cache itself is purged, some images which are held strongly by UIImageViews or other live instances can be recovered again, to avoid later re-query from disk cache or network. This may be helpful for the case, for example, when app enter background and memory is purged, cause cell flashing after re-enter foreground.
* Defautls to YES. You can change this option dynamically.
*/
@property (assign, nonatomic) BOOL shouldUseWeakMemoryCache;
/**
* Whether or not to remove the expired disk data when application entering the background. (Not works for macOS)
* Defatuls to YES.
@ -72,6 +91,12 @@
*/
@property (assign, nonatomic) NSUInteger maxMemoryCount;
/*
* The attribute which the clear cache will be checked against when clearing the disk cache
* Default is Modified Date
*/
@property (assign, nonatomic) SDImageCacheConfigExpireType diskCacheExpireType;
/**
* The namespace prefix of cache. It's used to prefix the namespace you provide to the caches's initializer. You 'd better name it with reverse domain name notation and keep the final dot.
* Defautls to `com.hackemist.SDImageCache.`, which will prefix your namespace such as `default` to final `com.hackemist.SDImageCache.default`. If you specify nil, it will be treated equals to an empty string.

View File

@ -27,11 +27,13 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
if (self = [super init]) {
_shouldDisableiCloud = YES;
_shouldCacheImagesInMemory = YES;
_shouldUseWeakMemoryCache = YES;
_shouldRemoveExpiredDataWhenEnterBackground = YES;
_diskCacheReadingOptions = 0;
_diskCacheWritingOptions = NSDataWritingAtomic;
_maxCacheAge = kDefaultCacheMaxCacheAge;
_maxCacheSize = 0;
_diskCacheExpireType = SDImageCacheConfigExpireTypeModificationDate;
_namespacePrefix = @"com.hackemist.SDImageCache.";
_memoryCacheClass = [SDMemoryCache class];
_diskCacheClass = [SDDiskCache class];
@ -43,6 +45,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
SDImageCacheConfig *config = [[[self class] allocWithZone:zone] init];
config.shouldDisableiCloud = self.shouldDisableiCloud;
config.shouldCacheImagesInMemory = self.shouldCacheImagesInMemory;
config.shouldUseWeakMemoryCache = self.shouldUseWeakMemoryCache;
config.shouldRemoveExpiredDataWhenEnterBackground = self.shouldRemoveExpiredDataWhenEnterBackground;
config.diskCacheReadingOptions = self.diskCacheReadingOptions;
config.diskCacheWritingOptions = self.diskCacheWritingOptions;
@ -50,6 +53,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
config.maxCacheSize = self.maxCacheSize;
config.maxMemoryCost = self.maxMemoryCost;
config.maxMemoryCount = self.maxMemoryCount;
config.diskCacheExpireType = self.diskCacheExpireType;
config.namespacePrefix = self.namespacePrefix;
config.fileManager = self.fileManager; // NSFileManager does not conform to NSCopying, just pass the reference
config.memoryCacheClass = self.memoryCacheClass;