diff --git a/SDWebImage/Core/UIImage+MemoryCacheCost.m b/SDWebImage/Core/UIImage+MemoryCacheCost.m index b0883b1f..4cf80bf8 100644 --- a/SDWebImage/Core/UIImage+MemoryCacheCost.m +++ b/SDWebImage/Core/UIImage+MemoryCacheCost.m @@ -20,7 +20,8 @@ FOUNDATION_STATIC_INLINE NSUInteger SDMemoryCacheCostForImage(UIImage *image) { #if SD_MAC frameCount = 1; #elif SD_UIKIT || SD_WATCH - frameCount = image.images.count > 0 ? image.images.count : 1; + // Filter the same frame in `_UIAnimatedImage`. + frameCount = image.images.count > 0 ? [NSSet setWithArray:image.images].count : 1; #endif NSUInteger cost = bytesPerFrame * frameCount; return cost; diff --git a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj index b9c1fda2..21c374b0 100644 --- a/Tests/SDWebImage Tests.xcodeproj/project.pbxproj +++ b/Tests/SDWebImage Tests.xcodeproj/project.pbxproj @@ -104,6 +104,7 @@ 4369C2741D9804B1007E863A /* SDWebCacheCategoriesTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 4369C2731D9804B1007E863A /* SDWebCacheCategoriesTests.m */; }; 43828A451DA67F9900000E62 /* TestImageLarge.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 43828A441DA67F9900000E62 /* TestImageLarge.jpg */; }; 5F7F38AD1AE2A77A00B0E330 /* TestImage.jpg in Resources */ = {isa = PBXBuildFile; fileRef = 5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */; }; + 6B181A1B265757ED00BD06B3 /* TestAnimatedImageMemory.webp in Resources */ = {isa = PBXBuildFile; fileRef = 6B181A1A265757ED00BD06B3 /* TestAnimatedImageMemory.webp */; }; 770D3B9C7914D53EBA0524DB /* Pods_Tests_TV.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B73996BC299E8E0174661628 /* Pods_Tests_TV.framework */; }; AFDBD4044E95252B70FB85CE /* Pods_Tests_iOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AD1BD830DA7388EB6C110B3B /* Pods_Tests_iOS.framework */; }; DA248D57195472AA00390AB0 /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DA248D56195472AA00390AB0 /* XCTest.framework */; }; @@ -165,6 +166,7 @@ 5B0011ED58CC9998E68ECCE3 /* Pods-Tests TV.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests TV.debug.xcconfig"; path = "../Pods/Target Support Files/Pods-Tests TV/Pods-Tests TV.debug.xcconfig"; sourceTree = ""; }; 5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = TestImage.jpg; sourceTree = ""; }; 6700DBD9C45B00AB5F77472A /* Pods-Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Tests/Pods-Tests.release.xcconfig"; sourceTree = ""; }; + 6B181A1A265757ED00BD06B3 /* TestAnimatedImageMemory.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestAnimatedImageMemory.webp; sourceTree = ""; }; 6CB13D7A081654B2E4018F53 /* Pods-Tests TV.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Tests TV.release.xcconfig"; path = "../Pods/Target Support Files/Pods-Tests TV/Pods-Tests TV.release.xcconfig"; sourceTree = ""; }; A39EDFED1067C297B91792CB /* Pods_Tests_Mac.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Tests_Mac.framework; sourceTree = BUILT_PRODUCTS_DIR; }; AD1BD830DA7388EB6C110B3B /* Pods_Tests_iOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Tests_iOS.framework; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -265,6 +267,7 @@ 3297A09E23374D1600814590 /* TestImageAnimated.heic */, 32515F9824AF1919005E8F79 /* TestImageAnimated.webp */, 32515F9724AF1919005E8F79 /* TestImageStatic.webp */, + 6B181A1A265757ED00BD06B3 /* TestAnimatedImageMemory.webp */, ); path = Images; sourceTree = ""; @@ -511,6 +514,7 @@ buildActionMask = 2147483647; files = ( 327A418C211D660600495442 /* TestImage.heic in Resources */, + 6B181A1B265757ED00BD06B3 /* TestAnimatedImageMemory.webp in Resources */, 5F7F38AD1AE2A77A00B0E330 /* TestImage.jpg in Resources */, 32905E64211D786E00460FCF /* TestImage.heif in Resources */, 43828A451DA67F9900000E62 /* TestImageLarge.jpg in Resources */, diff --git a/Tests/Tests/Images/TestAnimatedImageMemory.webp b/Tests/Tests/Images/TestAnimatedImageMemory.webp new file mode 100644 index 00000000..0cd2f0ee Binary files /dev/null and b/Tests/Tests/Images/TestAnimatedImageMemory.webp differ diff --git a/Tests/Tests/SDAnimatedImageTest.m b/Tests/Tests/SDAnimatedImageTest.m index ef082285..7d93b32a 100644 --- a/Tests/Tests/SDAnimatedImageTest.m +++ b/Tests/Tests/SDAnimatedImageTest.m @@ -713,7 +713,7 @@ static BOOL _isCalled; [self waitForExpectationsWithTimeout:15 handler:nil]; } -- (void)test35AnimatedImagePlaybackModeReversedBounce{ +- (void)test35AnimatedImagePlaybackModeReversedBounce { XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView playback reverse bounce mode"]; SDAnimatedImageView *imageView = [SDAnimatedImageView new]; @@ -765,6 +765,18 @@ static BOOL _isCalled; [self waitForExpectationsWithTimeout:15 handler:nil]; } +- (void)test36AnimatedImageMemoryCost { + if (@available(iOS 14, tvOS 14, macOS 11, watchOS 7, *)) { + [[SDImageCodersManager sharedManager] addCoder:[SDImageAWebPCoder sharedCoder]]; + UIImage *image = [UIImage sd_imageWithData:[NSData dataWithContentsOfFile:[self testMemotyCostImagePath]]]; + NSUInteger cost = [image sd_memoryCost]; + expect(image.images.count).equal(5333); + expect(image.scale).equal(1); + expect(cost).equal(16 * image.size.width * image.size.height * 4); + [[SDImageCodersManager sharedManager] removeCoder:[SDImageAWebPCoder sharedCoder]]; + } +} + #pragma mark - Helper - (UIWindow *)window { if (!_window) { @@ -795,6 +807,12 @@ static BOOL _isCalled; return testPath; } +- (NSString *)testMemotyCostImagePath { + NSBundle *testBundle = [NSBundle bundleForClass:[self class]]; + NSString *testPath = [testBundle pathForResource:@"TestAnimatedImageMemory" ofType:@"webp"]; + return testPath; +} + - (NSData *)testAPNGPData { return [NSData dataWithContentsOfFile:[self testAPNGPPath]]; }