From 2c9eaccf237a247ab94e4cec2c20d82f5903c648 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 27 Aug 2020 10:49:57 +0800 Subject: [PATCH] Remove the `SDWebImageForceTransitionAsync`, change the default behavior because it's most suitable for UI rendering and common use case. No need this hack. If user want, we may produce a `setTransitionBlock` in the future --- Examples/SDWebImage OSX Demo/ViewController.m | 2 +- SDWebImage/Core/SDWebImageDefine.h | 13 +++---------- SDWebImage/Core/UIView+WebCache.m | 19 ++++++++----------- Tests/Tests/SDWebCacheCategoriesTests.m | 10 +++++----- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/Examples/SDWebImage OSX Demo/ViewController.m b/Examples/SDWebImage OSX Demo/ViewController.m index b59f236f..652a61a3 100644 --- a/Examples/SDWebImage OSX Demo/ViewController.m +++ b/Examples/SDWebImage OSX Demo/ViewController.m @@ -47,7 +47,7 @@ self.imageView4.sd_imageTransition = SDWebImageTransition.fadeTransition; self.imageView4.imageScaling = NSImageScaleProportionallyUpOrDown; self.imageView4.imageAlignment = NSImageAlignLeft; // supports NSImageView's layout properties - [self.imageView4 sd_setImageWithURL:[NSURL URLWithString:@"http://littlesvr.ca/apng/images/SteamEngine.webp"] placeholderImage:nil options:SDWebImageForceTransition]; + [self.imageView4 sd_setImageWithURL:[NSURL URLWithString:@"http://littlesvr.ca/apng/images/SteamEngine.webp"]]; NSMenu *menu2 = [[NSMenu alloc] initWithTitle:@"Toggle Animation"]; NSMenuItem *item2 = [menu2 addItemWithTitle:@"Toggle Animation" action:@selector(toggleAnimation:) keyEquivalent:@""]; item2.tag = 2; diff --git a/SDWebImage/Core/SDWebImageDefine.h b/SDWebImage/Core/SDWebImageDefine.h index d0ab2ec2..43df0464 100644 --- a/SDWebImage/Core/SDWebImageDefine.h +++ b/SDWebImage/Core/SDWebImageDefine.h @@ -160,8 +160,8 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { SDWebImageFromLoaderOnly = 1 << 16, /** - * 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`. + * By default, when you use `SDWebImageTransition` to do some view transition after the image load finished, this transition is only applied for image when the callback from manager is asynchronous (from network, or disk cache query) + * This mask can force to apply view transition for any cases, like memory cache query, or sync disk cache query. */ SDWebImageForceTransition = 1 << 17, @@ -201,14 +201,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { * We usually don't apply transform on vector images, because vector images supports dynamically changing to any size, rasterize to a fixed size will loss details. To modify vector images, you can process the vector data at runtime (such as modifying PDF tag / SVG element). * 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 disk cache hit (and you don't use `queryDiskDataSync`), this will trigger transition because disk query is asynchronous. The default behavior (without any options) only do transition when network query successed. - * @note This is best used for UI rendering common cases, because even the cache is from disk, we may see a quick flashing of placeholder. In summary, if user can see any waiting (whether it takes 10 milliseconds or 10 minutes), do transition. else not. - */ - SDWebImageForceTransitionAsync = 1 << 24 + SDWebImageTransformVectorImage = 1 << 23 }; diff --git a/SDWebImage/Core/UIView+WebCache.m b/SDWebImage/Core/UIView+WebCache.m index c309b5e6..befff482 100644 --- a/SDWebImage/Core/UIView+WebCache.m +++ b/SDWebImage/Core/UIView+WebCache.m @@ -179,23 +179,20 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; // Always shouldUseTransition = YES; } else if (cacheType == SDImageCacheTypeNone) { - // Default, from network + // From network shouldUseTransition = YES; } else { - // Async, from disk (and, user don't use sync query) - if (options & SDWebImageForceTransitionAsync) { - if (cacheType == SDImageCacheTypeMemory) { + // From disk (and, user don't use sync query) + if (cacheType == SDImageCacheTypeMemory) { + shouldUseTransition = NO; + } else if (cacheType == SDImageCacheTypeDisk) { + if (options & SDWebImageQueryMemoryDataSync || options & SDWebImageQueryDiskDataSync) { shouldUseTransition = NO; - } else if (cacheType == SDImageCacheTypeDisk) { - if (options & SDWebImageQueryMemoryDataSync || options & SDWebImageQueryDiskDataSync) { - shouldUseTransition = NO; - } else { - shouldUseTransition = YES; - } } else { - shouldUseTransition = NO; + shouldUseTransition = YES; } } else { + // Not valid cache type, fallback shouldUseTransition = NO; } } diff --git a/Tests/Tests/SDWebCacheCategoriesTests.m b/Tests/Tests/SDWebCacheCategoriesTests.m index f7213437..f27af474 100644 --- a/Tests/Tests/SDWebCacheCategoriesTests.m +++ b/Tests/Tests/SDWebCacheCategoriesTests.m @@ -253,8 +253,8 @@ [self waitForExpectationsWithCommonTimeout]; } -- (void)testUIViewTransitionWork { - XCTestExpectation *expectation = [self expectationWithDescription:@"UIView transition does not work"]; +- (void)testUIViewTransitionFromNetworkWork { + XCTestExpectation *expectation = [self expectationWithDescription:@"UIView transition from network does not work"]; // Attach a window, or CALayer will not submit drawing UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; @@ -296,8 +296,8 @@ [self waitForExpectationsWithCommonTimeout]; } -- (void)testUIViewTransitionAsyncWork { - XCTestExpectation *expectation = [self expectationWithDescription:@"UIView transition async does not work"]; +- (void)testUIViewTransitionFromDiskWork { + XCTestExpectation *expectation = [self expectationWithDescription:@"UIView transition from disk does not work"]; // Attach a window, or CALayer will not submit drawing UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; @@ -323,7 +323,7 @@ __weak typeof(imageView) wimageView = imageView; [imageView sd_setImageWithURL:originalImageURL placeholderImage:placeholder - options:SDWebImageForceTransitionAsync | SDWebImageFromCacheOnly // Ensure we queired from disk cache + options:SDWebImageFromCacheOnly // Ensure we queired from disk cache completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { [SDImageCache.sharedImageCache removeImageFromMemoryForKey:kTestJPEGURL]; [SDImageCache.sharedImageCache removeImageFromDiskForKey:kTestJPEGURL];