From 3c89a092cd1f14d989d819e13cf411d27dce151b Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 3 Sep 2024 17:22:34 +0800 Subject: [PATCH] Added the blend mode to transformer API as well Update unit tests --- SDWebImage/Core/SDImageTransformer.h | 3 +++ SDWebImage/Core/SDImageTransformer.m | 8 +++++++- Tests/Tests/SDImageTransformerTests.m | 6 +++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/SDWebImage/Core/SDImageTransformer.h b/SDWebImage/Core/SDImageTransformer.h index 31b4370e..0c0ff4f1 100644 --- a/SDWebImage/Core/SDImageTransformer.h +++ b/SDWebImage/Core/SDImageTransformer.h @@ -223,11 +223,14 @@ FOUNDATION_EXPORT NSString * _Nullable SDThumbnailedKeyForKey(NSString * _Nullab The tint color. */ @property (nonatomic, strong, readonly, nonnull) UIColor *tintColor; +/// The blend mode, defaults to `sourceAtop` if you use the old initializer +@property (nonatomic, assign, readonly) CGBlendMode blendMode; - (nonnull instancetype)init NS_UNAVAILABLE; + (nonnull instancetype)new NS_UNAVAILABLE; + (nonnull instancetype)transformerWithColor:(nonnull UIColor *)tintColor; ++ (nonnull instancetype)transformerWithColor:(nonnull UIColor *)tintColor blendMode:(CGBlendMode)blendMode; @end diff --git a/SDWebImage/Core/SDImageTransformer.m b/SDWebImage/Core/SDImageTransformer.m index 8e7a3e2a..056cbbdd 100644 --- a/SDWebImage/Core/SDImageTransformer.m +++ b/SDWebImage/Core/SDImageTransformer.m @@ -245,20 +245,26 @@ NSString * _Nullable SDThumbnailedKeyForKey(NSString * _Nullable key, CGSize thu @interface SDImageTintTransformer () @property (nonatomic, strong, nonnull) UIColor *tintColor; +@property (nonatomic, assign) CGBlendMode blendMode; @end @implementation SDImageTintTransformer + (instancetype)transformerWithColor:(UIColor *)tintColor { + return [self transformerWithColor:tintColor blendMode:kCGBlendModeSourceAtop]; +} + ++ (instancetype)transformerWithColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode { SDImageTintTransformer *transformer = [SDImageTintTransformer new]; transformer.tintColor = tintColor; + transformer.blendMode = blendMode; return transformer; } - (NSString *)transformerKey { - return [NSString stringWithFormat:@"SDImageTintTransformer(%@)", self.tintColor.sd_hexString]; + return [NSString stringWithFormat:@"SDImageTintTransformer(%@,%d)", self.tintColor.sd_hexString, self.blendMode]; } - (UIImage *)transformedImageWithImage:(UIImage *)image forKey:(NSString *)key { diff --git a/Tests/Tests/SDImageTransformerTests.m b/Tests/Tests/SDImageTransformerTests.m index 7ad802f9..1559d4d2 100644 --- a/Tests/Tests/SDImageTransformerTests.m +++ b/Tests/Tests/SDImageTransformerTests.m @@ -242,6 +242,10 @@ static void SDAssertCGImageFirstComponentWhite(CGImageRef image, OSType pixelTyp // Check rounded corner operation not inversion the image UIColor *topCenterColor = [tintedImage sd_colorAtPoint:CGPointMake(150, 20)]; expect([topCenterColor.sd_hexString isEqualToString:[UIColor blackColor].sd_hexString]).beTruthy(); + + UIImage *tintedSourceInImage = [testImage sd_tintedImageWithColor:tintColor blendMode:kCGBlendModeSourceOut]; + centerColor = [tintedSourceInImage sd_colorAtPoint:CGPointMake(150, 150)]; + expect([centerColor.sd_hexString isEqualToString:[UIColor blackColor].sd_hexString]).beTruthy(); } - (void)test07UIImageTransformBlurCG { @@ -353,7 +357,7 @@ static void SDAssertCGImageFirstComponentWhite(CGImageRef image, OSType pixelTyp @"SDImageRoundCornerTransformer(50.000000,18446744073709551615,1.000000,#ff000000)", @"SDImageFlippingTransformer(1,1)", @"SDImageCroppingTransformer({0.000000,0.000000,50.000000,50.000000})", - @"SDImageTintTransformer(#00000000)", + @"SDImageTintTransformer(#00000000,20)", @"SDImageBlurTransformer(5.000000)", @"SDImageFilterTransformer(CIColorInvert)" ];