From dd2e71c99dcb77c5f4e57def791d60b58338a2c7 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 17 Apr 2018 12:03:24 +0800 Subject: [PATCH] Add support for SDAnimatedImage imageNamed:bundle: for macOS, enable SDAnimatedImageTest for macOS --- SDWebImage/SDAnimatedImage.h | 2 ++ SDWebImage/SDAnimatedImage.m | 6 +++- .../project.pbxproj | 2 ++ Tests/Tests/SDAnimatedImageTest.m | 29 +++++++++++++++++-- 4 files changed, 35 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDAnimatedImage.h b/SDWebImage/SDAnimatedImage.h index bb452669..92282f56 100644 --- a/SDWebImage/SDAnimatedImage.h +++ b/SDWebImage/SDAnimatedImage.h @@ -54,6 +54,8 @@ + (nullable instancetype)imageNamed:(nonnull NSString *)name; // Cache in memory, no Asset Catalog support #if __has_include() + (nullable instancetype)imageNamed:(nonnull NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection; // Cache in memory, no Asset Catalog support +#else ++ (nullable instancetype)imageNamed:(nonnull NSString *)name inBundle:(nullable NSBundle *)bundle; // Cache in memory, no Asset Catalog support #endif + (nullable instancetype)imageWithContentsOfFile:(nonnull NSString *)path; + (nullable instancetype)imageWithData:(nonnull NSData *)data; diff --git a/SDWebImage/SDAnimatedImage.m b/SDWebImage/SDAnimatedImage.m index 34d88051..c54dc112 100644 --- a/SDWebImage/SDAnimatedImage.m +++ b/SDWebImage/SDAnimatedImage.m @@ -212,7 +212,7 @@ static NSArray *SDBundlePreferredScales() { #if __has_include() return [self imageNamed:name inBundle:nil compatibleWithTraitCollection:nil]; #else - return [self imageNamed:name inBundle:nil scale:0]; + return [self imageNamed:name inBundle:nil]; #endif } @@ -224,6 +224,10 @@ static NSArray *SDBundlePreferredScales() { CGFloat scale = traitCollection.displayScale; return [self imageNamed:name inBundle:bundle scale:scale]; } +#else ++ (instancetype)imageNamed:(NSString *)name inBundle:(NSBundle *)bundle { + return [self imageNamed:name inBundle:bundle scale:0]; +} #endif // 0 scale means automatically check diff --git a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj index 940677b8..c34ad2bc 100644 --- a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj +++ b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 0314594336AFF15E5BB7F0E6 /* Pods_Tests_Mac.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = C140E5ED8501C2ABBFD97A24 /* Pods_Tests_Mac.framework */; }; 1E3C51E919B46E370092B5E6 /* SDWebImageDownloaderTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 1E3C51E819B46E370092B5E6 /* SDWebImageDownloaderTests.m */; }; 2D7AF0601F329763000083C2 /* SDTestCase.m in Sources */ = {isa = PBXBuildFile; fileRef = 2D7AF05F1F329763000083C2 /* SDTestCase.m */; }; + 320630412085A37C006E0FA4 /* SDAnimatedImageTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 32A571552037DB2D002EDAAE /* SDAnimatedImageTest.m */; }; 321259EC1F39E3240096FE0E /* TestImageStatic.webp in Resources */ = {isa = PBXBuildFile; fileRef = 321259EB1F39E3240096FE0E /* TestImageStatic.webp */; }; 321259EE1F39E4110096FE0E /* TestImageAnimated.webp in Resources */ = {isa = PBXBuildFile; fileRef = 321259ED1F39E4110096FE0E /* TestImageAnimated.webp */; }; 3226ECBB20754F7700FAFACF /* SDWebImageTestDownloadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 3226ECBA20754F7700FAFACF /* SDWebImageTestDownloadOperation.m */; }; @@ -474,6 +475,7 @@ 32B99EAB203B36620017FD66 /* SDWebImageManagerTests.m in Sources */, 32B99EA9203B34B60017FD66 /* SDWebImageDecoderTests.m in Sources */, 3264FF30205D42CB00F6BD48 /* SDWebImageTestTransformer.m in Sources */, + 320630412085A37C006E0FA4 /* SDAnimatedImageTest.m in Sources */, 32B99E9B203B2EDD0017FD66 /* SDTestCase.m in Sources */, 3226ECBC20754F7700FAFACF /* SDWebImageTestDownloadOperation.m in Sources */, ); diff --git a/Tests/Tests/SDAnimatedImageTest.m b/Tests/Tests/SDAnimatedImageTest.m index add6c40a..0434b948 100644 --- a/Tests/Tests/SDAnimatedImageTest.m +++ b/Tests/Tests/SDAnimatedImageTest.m @@ -15,6 +15,11 @@ #import #import +#if SD_MAC +#define UIWindow NSWindow +#define UIScreen NSScreen +#endif + static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop count // Internal header @@ -69,7 +74,12 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun } - (void)test04AnimatedImageImageNamed { - SDAnimatedImage *image = [SDAnimatedImage imageNamed:@"TestImage.gif" inBundle:[NSBundle bundleForClass:[self class]] compatibleWithTraitCollection:nil]; + NSBundle *bundle = [NSBundle bundleForClass:[self class]]; +#if SD_UIKIT + SDAnimatedImage *image = [SDAnimatedImage imageNamed:@"TestImage.gif" inBundle:bundle compatibleWithTraitCollection:nil]; +#else + SDAnimatedImage *image = [SDAnimatedImage imageNamed:@"TestImage.gif" inBundle:bundle]; +#endif expect(image).notTo.beNil(); expect([image.animatedImageData isEqualToData:[self testGIFData]]).beTruthy(); } @@ -91,7 +101,7 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun - (void)test06AnimatedImageViewSetImage { SDAnimatedImageView *imageView = [SDAnimatedImageView new]; - UIImage *image = [UIImage imageWithData:[self testJPEGData]]; + UIImage *image = [[UIImage alloc] initWithData:[self testJPEGData]]; imageView.image = image; expect(imageView.image).notTo.beNil(); expect(imageView.currentFrame).beNil(); // current frame @@ -108,7 +118,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun - (void)test08AnimatedImageViewRendering { XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView rendering"]; SDAnimatedImageView *imageView = [[SDAnimatedImageView alloc] init]; +#if SD_UIKIT [self.window addSubview:imageView]; +#else + [self.window.contentView addSubview:imageView]; +#endif NSMutableDictionary *frames = [NSMutableDictionary dictionaryWithCapacity:kTestGIFFrameCount]; @@ -128,7 +142,11 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun loopFinished = YES; } if (framesRendered && loopFinished) { +#if SD_UIKIT [imageView stopAnimating]; +#else + imageView.animates = NO; +#endif [expectation fulfill]; } }]; @@ -205,7 +223,12 @@ static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop coun #pragma mark - Helper - (UIWindow *)window { if (!_window) { - _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; + UIScreen *mainScreen = [UIScreen mainScreen]; +#if SD_UIKIT + _window = [[UIWindow alloc] initWithFrame:mainScreen.bounds]; +#else + _window = [[NSWindow alloc] initWithContentRect:mainScreen.frame styleMask:0 backing:NSBackingStoreBuffered defer:NO screen:mainScreen]; +#endif } return _window; }