Merge pull request #3237 from kinarobin/fix-wrong-memory-cost

Fix wrong memory cost in `_UIAnimatedImage`
This commit is contained in:
DreamPiggy 2021-05-21 15:37:27 +08:00 committed by GitHub
commit 63a3d0b996
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 2 deletions

View File

@ -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;

View File

@ -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 = "<group>"; };
5F7F38AC1AE2A77A00B0E330 /* TestImage.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = TestImage.jpg; sourceTree = "<group>"; };
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 = "<group>"; };
6B181A1A265757ED00BD06B3 /* TestAnimatedImageMemory.webp */ = {isa = PBXFileReference; lastKnownFileType = file; path = TestAnimatedImageMemory.webp; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>";
@ -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 */,

Binary file not shown.

After

Width:  |  Height:  |  Size: 574 KiB

View File

@ -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]];
}