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/SDImageCache.m b/SDWebImage/Core/SDImageCache.m index 25b39e97..c2f18a9d 100644 --- a/SDWebImage/Core/SDImageCache.m +++ b/SDWebImage/Core/SDImageCache.m @@ -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) { @@ -545,10 +542,10 @@ static NSString * _defaultDiskCacheDirectory; if (doneBlock) { if (shouldQueryDiskSync) { - doneBlock(diskImage, diskData, cacheType); + doneBlock(diskImage, diskData, SDImageCacheTypeDisk); } else { dispatch_async(dispatch_get_main_queue(), ^{ - doneBlock(diskImage, diskData, cacheType); + doneBlock(diskImage, diskData, SDImageCacheTypeDisk); }); } } diff --git a/SDWebImage/Core/SDWebImageDefine.h b/SDWebImage/Core/SDWebImageDefine.h index 90ee605f..43df0464 100644 --- a/SDWebImage/Core/SDWebImageDefine.h +++ b/SDWebImage/Core/SDWebImageDefine.h @@ -160,7 +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. + * 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, @@ -200,7 +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, + SDWebImageTransformVectorImage = 1 << 23 }; diff --git a/SDWebImage/Core/UIView+WebCache.m b/SDWebImage/Core/UIView+WebCache.m index 160a77aa..befff482 100644 --- a/SDWebImage/Core/UIView+WebCache.m +++ b/SDWebImage/Core/UIView+WebCache.m @@ -174,7 +174,29 @@ 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) { + // From network + shouldUseTransition = YES; + } else { + // 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 { + shouldUseTransition = YES; + } + } else { + // Not valid cache type, fallback + shouldUseTransition = NO; + } + } + if (finished && shouldUseTransition) { transition = self.sd_imageTransition; } #endif diff --git a/Tests/Tests/SDWebCacheCategoriesTests.m b/Tests/Tests/SDWebCacheCategoriesTests.m index 666037dd..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,6 +296,50 @@ [self waitForExpectationsWithCommonTimeout]; } +- (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)]; + imageView.sd_imageTransition = SDWebImageTransition.fadeTransition; + imageView.sd_imageTransition.duration = 1; + +#if SD_UIKIT + [self.window addSubview:imageView]; +#else + imageView.wantsLayer = YES; + [self.window.contentView addSubview:imageView]; +#endif + + NSData *imageData = [NSData dataWithContentsOfFile:[self testJPEGPath]]; + UIImage *placeholder = [[UIImage alloc] initWithData:imageData]; + + // Ensure the image is cached in disk but not memory + [SDImageCache.sharedImageCache removeImageFromMemoryForKey:kTestJPEGURL]; + [SDImageCache.sharedImageCache removeImageFromDiskForKey:kTestJPEGURL]; + [SDImageCache.sharedImageCache storeImageDataToDisk:imageData forKey:kTestJPEGURL]; + + NSURL *originalImageURL = [NSURL URLWithString:kTestJPEGURL]; + __weak typeof(imageView) wimageView = imageView; + [imageView sd_setImageWithURL:originalImageURL + placeholderImage:placeholder + 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]; + __strong typeof(wimageView) simageView = imageView; + // Delay to let CALayer commit the transition in next runloop + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, kMinDelayNanosecond), dispatch_get_main_queue(), ^{ + // Check current view contains layer animation + NSArray *animationKeys = simageView.layer.animationKeys; + expect(animationKeys.count).beGreaterThan(0); + [expectation fulfill]; + }); + }]; + + [self waitForExpectationsWithCommonTimeout]; +} + - (void)testUIViewIndicatorWork { XCTestExpectation *expectation = [self expectationWithDescription:@"UIView indicator does not work"];