Refactory the current behavior to use transition. Introduce `SDWebImageForceTransitionAsync` (compared to old one `SDWebImageForceTransitionAlways`)

This commit is contained in:
DreamPiggy 2020-08-24 16:23:51 +08:00
parent 35578f0524
commit df0f6008fc
3 changed files with 35 additions and 4 deletions

View File

@ -528,13 +528,10 @@ static NSString * _defaultDiskCacheDirectory;
@autoreleasepool {
NSData *diskData = [self diskImageDataBySearchingAllPathsForKey:key];
UIImage *diskImage;
SDImageCacheType cacheType = SDImageCacheTypeNone;
if (image) {
// the image is from in-memory cache, but need image data
diskImage = image;
cacheType = SDImageCacheTypeMemory;
} else if (diskData) {
cacheType = SDImageCacheTypeDisk;
// decode image data only if in-memory cache missed
diskImage = [self diskImageForKey:key data:diskData options:options context:context];
if (diskImage && self.config.shouldCacheImagesInMemory) {
@ -542,6 +539,7 @@ static NSString * _defaultDiskCacheDirectory;
[self.memoryCache setObject:diskImage forKey:key cost:cost];
}
}
SDImageCacheType cacheType = diskImage ? SDImageCacheTypeDisk : SDImageCacheTypeNone;
if (doneBlock) {
if (shouldQueryDiskSync) {

View File

@ -161,6 +161,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
/**
* By default, when you use `SDWebImageTransition` to do some view transition after the image load finished, this transition is only applied for image download from the network. This mask can force to apply view transition for memory and disk cache as well.
* @note This options naming may be `SDWebImageForceTransitionAlways` in the furture. Which does not check any condition, just do transition even we query the cache immediately from memory. See related `SDWebImageForceTransitionAsync`.
*/
SDWebImageForceTransition = 1 << 17,
@ -201,6 +202,13 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
* Use this flag to transform them anyway.
*/
SDWebImageTransformVectorImage = 1 << 23,
/**
* By default, when you use `SDWebImageTransition` to do some view transition after the image load finished, this transition is only applied for image download from the network. This mask can force to apply view transition for condition when the callback from manager is asynchronous.
* For example, when memory cache hit, or disk cache who using `queryDiskDataSync`, this will trigger transition. The default behavior (without any options) only do transition when network query successed.
* @note This is used for UI rendering which relay the same runloop to avoid flashing, suitable for common use case cases. Which means, if user can see any waiting, do transition. else not.
*/
SDWebImageForceTransitionAsync = 1 << 24
};

View File

@ -174,7 +174,32 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
#if SD_UIKIT || SD_MAC
// check whether we should use the image transition
SDWebImageTransition *transition = nil;
if (finished && (options & SDWebImageForceTransition || cacheType == SDImageCacheTypeNone)) {
BOOL shouldUseTransition = NO;
if (options & SDWebImageForceTransition) {
// Always
shouldUseTransition = YES;
} else if (cacheType == SDImageCacheTypeNone) {
// Default, from network
shouldUseTransition = YES;
} else {
// Async, from disk (and, user don't use sync query)
if (options & SDWebImageForceTransitionAsync) {
if (cacheType == SDImageCacheTypeMemory) {
shouldUseTransition = NO;
} else if (cacheType == SDImageCacheTypeDisk) {
if (options & SDWebImageQueryMemoryDataSync || options & SDWebImageQueryDiskDataSync) {
shouldUseTransition = NO;
} else {
shouldUseTransition = YES;
}
} else {
shouldUseTransition = NO;
}
} else {
shouldUseTransition = NO;
}
}
if (finished && shouldUseTransition) {
transition = self.sd_imageTransition;
}
#endif