Skip to content

Commit e669dee

Browse files
committed
Added test case for animationTransformer
1 parent 41dc9be commit e669dee

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

SDWebImage/Core/SDAnimatedImageView.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ NS_SWIFT_UI_ACTOR
3232
/**
3333
The transformer for each decoded animated image frame.
3434
We supports post-transform on animated image frame from version 5.20.
35-
When you configure the transformer on `SDAnimatedImageView` and animation is playing, the `transformedImageWithImage:forKey:` will be called just after the frame is decoded. (The `key` arg is always empty for backward-compatible)
35+
When you configure the transformer on `SDAnimatedImageView` and animation is playing, the `transformedImageWithImage:forKey:` will be called just after the frame is decoded. (note: The `key` arg is always empty for backward-compatible and may be removed in the future)
3636
37-
Example to tint the animated image with alpha channel into template:
37+
Example to tint the alpha animated image frame with a black color:
3838
* @code
3939
imageView.animationTransformer = [SDImageTintTransformer transformerWithColor:UIColor.blackColor];
4040
* @endcode
41-
@note The `transformerKey` property is used to ensure the buffer cache available. So make sure it's correct value match the actual logic on transformer.
41+
@note The `transformerKey` property is used to ensure the buffer cache available. So make sure it's correct value match the actual logic on transformer. Which means, for the `same frame index + same transformer key`, the transformed image should always be the same.
4242
*/
4343
@property (nonatomic, strong, nullable) id<SDImageTransformer> animationTransformer;
4444

Tests/Tests/SDAnimatedImageTest.m

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import "SDTestCase.h"
1111
#import "SDInternalMacros.h"
1212
#import "SDImageFramePool.h"
13+
#import "SDWebImageTestTransformer.h"
1314
#import <KVOController/KVOController.h>
1415

1516
static const NSUInteger kTestGIFFrameCount = 5; // local TestImage.gif loop count
@@ -808,6 +809,40 @@ - (void)test37AnimatedImageWithStaticDataBehavior {
808809
expect(scaledImage).notTo.equal(image);
809810
}
810811

812+
- (void)testAnimationTransformerWorks {
813+
XCTestExpectation *expectation = [self expectationWithDescription:@"test SDAnimatedImageView animationTransformer works"];
814+
SDAnimatedImageView *imageView = [SDAnimatedImageView new];
815+
// Setup transformer, showing which hook all frames into the test image
816+
UIImage *testImage = [[UIImage alloc] initWithData:[self testJPEGData]];
817+
SDWebImageTestTransformer *transformer = [SDWebImageTestTransformer new];
818+
transformer.testImage = testImage;
819+
imageView.animationTransformer = transformer;
820+
821+
#if SD_UIKIT
822+
[self.window addSubview:imageView];
823+
#else
824+
[self.window.contentView addSubview:imageView];
825+
#endif
826+
SDAnimatedImage *image = [SDAnimatedImage imageWithData:[self testGIFData]];
827+
imageView.image = image;
828+
#if SD_UIKIT
829+
[imageView startAnimating];
830+
#else
831+
imageView.animates = YES;
832+
#endif
833+
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
834+
// 0.5s is not finished, frame index should not be 0
835+
expect(imageView.player.framePool.currentFrameCount).beGreaterThan(0);
836+
expect(imageView.currentFrameIndex).beGreaterThan(0);
837+
// Test the current frame image is hooked by transformer
838+
expect(imageView.currentFrame).equal(testImage);
839+
840+
[expectation fulfill];
841+
});
842+
843+
[self waitForExpectationsWithCommonTimeout];
844+
}
845+
811846
#pragma mark - Helper
812847

813848
- (NSString *)testGIFPath {

0 commit comments

Comments
 (0)