Skip to content

Commit 05e1840

Browse files
committed
Behavior changes: change the default tint transformer to use sourceIn instead of sourceAtop
This matches the Apple UIKit's naming and behavior
1 parent 7892f4c commit 05e1840

File tree

5 files changed

+34
-9
lines changed

5 files changed

+34
-9
lines changed

SDWebImage/Core/SDImageTransformer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ FOUNDATION_EXPORT NSString * _Nullable SDThumbnailedKeyForKey(NSString * _Nullab
223223
The tint color.
224224
*/
225225
@property (nonatomic, strong, readonly, nonnull) UIColor *tintColor;
226-
/// The blend mode, defaults to `sourceAtop` if you use the old initializer
226+
/// The blend mode, defaults to `sourceIn` if you use the initializer without blend mode
227227
@property (nonatomic, assign, readonly) CGBlendMode blendMode;
228228

229229
- (nonnull instancetype)init NS_UNAVAILABLE;

SDWebImage/Core/SDImageTransformer.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ @interface SDImageTintTransformer ()
252252
@implementation SDImageTintTransformer
253253

254254
+ (instancetype)transformerWithColor:(UIColor *)tintColor {
255-
return [self transformerWithColor:tintColor blendMode:kCGBlendModeSourceAtop];
255+
return [self transformerWithColor:tintColor blendMode:kCGBlendModeSourceIn];
256256
}
257257

258258
+ (instancetype)transformerWithColor:(UIColor *)tintColor blendMode:(CGBlendMode)blendMode {

SDWebImage/Core/UIImage+Transform.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ typedef NS_OPTIONS(NSUInteger, SDRectCorner) {
9898
#pragma mark - Image Blending
9999

100100
/**
101-
Return a tinted image with the given color. This actually use `sourceAtop` blend mode.
101+
Return a tinted image with the given color. This actually use `sourceIn` blend mode.
102+
@note Before 5.20, this API actually use `sourceAtop` and cause naming confusing. After 5.20, we match UIKit's behavior using `sourceIn`.
102103
103104
@param tintColor The tint color.
104105
@return The new image with the tint color.
@@ -107,7 +108,7 @@ typedef NS_OPTIONS(NSUInteger, SDRectCorner) {
107108

108109
/**
109110
Return a tinted image with the given color and blend mode.
110-
@note The blend mode treat `self` as background image (destination), treat `tintColor` as input image (source). So mostly you need `source` variant blend mode (use `sourceAtop` not `destinationAtop`).
111+
@note The blend mode treat `self` as background image (destination), treat `tintColor` as input image (source). So mostly you need `source` variant blend mode (use `sourceIn` not `destinationIn`), which is different from UIKit's `+[UIImage imageWithTintColor:]`.
111112
112113
@param tintColor The tint color.
113114
@param blendMode The blend mode.

SDWebImage/Core/UIImage+Transform.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ - (nullable UIImage *)sd_flippedImageWithHorizontal:(BOOL)horizontal vertical:(B
627627
}
628628

629629
- (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor {
630-
return [self sd_tintedImageWithColor:tintColor blendMode:kCGBlendModeSourceAtop];
630+
return [self sd_tintedImageWithColor:tintColor blendMode:kCGBlendModeSourceIn];
631631
}
632632

633633
- (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor blendMode:(CGBlendMode)blendMode {
@@ -694,7 +694,7 @@ - (nullable UIImage *)sd_tintedImageWithColor:(nonnull UIColor *)tintColor blend
694694
// MAX
695695
ciImage = [ciImage imageByApplyingFilter:@"CIColorClamp" withInputParameters:nil];
696696
} else {
697-
SD_LOG("UIImage+Transform error: Unsupported blend mode: %zu", blendMode);
697+
SD_LOG("UIImage+Transform error: Unsupported blend mode: %d", blendMode);
698698
ciImage = nil;
699699
}
700700
}

Tests/Tests/SDImageTransformerTests.m

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,33 @@ - (void)test06UIImageTransformTintWithImage:(UIImage *)testImage {
244244
expect([topCenterColor.sd_hexString isEqualToString:[UIColor blackColor].sd_hexString]).beTruthy();
245245

246246
UIImage *tintedSourceInImage = [testImage sd_tintedImageWithColor:tintColor blendMode:kCGBlendModeSourceIn];
247-
centerColor = [tintedSourceInImage sd_colorAtPoint:CGPointMake(150, 150)];
248-
expect([centerColor.sd_hexString isEqualToString:[UIColor blackColor].sd_hexString]).beTruthy();
247+
topCenterColor = [tintedSourceInImage sd_colorAtPoint:CGPointMake(150, 20)];
248+
#if SD_UIKIT
249+
// Test UIKit's tint color behavior
250+
if (@available(iOS 13.0, tvOS 13.0, watchOS 6.0, *)) {
251+
UIImage *tintedSystemImage = [testImage imageWithTintColor:tintColor renderingMode:UIImageRenderingModeAlwaysTemplate];
252+
UIGraphicsImageRendererFormat *format = UIGraphicsImageRendererFormat.preferredFormat;
253+
format.scale = tintedSourceInImage.scale;
254+
UIGraphicsImageRenderer *renderer = [[UIGraphicsImageRenderer alloc] initWithSize:tintedSystemImage.size format:format];
255+
// Draw template image
256+
tintedSystemImage = [renderer imageWithActions:^(UIGraphicsImageRendererContext * _Nonnull rendererContext) {
257+
[tintedSystemImage drawInRect:CGRectMake(0, 0, tintedSystemImage.size.width, tintedSystemImage.size.height)];
258+
}];
259+
UIColor *testColor1 = [tintedSourceInImage sd_colorAtPoint:CGPointMake(150, 20)];
260+
UIColor *testColor2 = [tintedSystemImage sd_colorAtPoint:CGPointMake(150, 20)];
261+
CGFloat r1, g1, b1, a1;
262+
CGFloat r2, g2, b2, a2;
263+
[testColor1 getRed:&r1 green:&g1 blue:&b1 alpha:&a1];
264+
[testColor2 getRed:&r2 green:&g2 blue:&b2 alpha:&a2];
265+
expect(r1).beCloseToWithin(r2, 0.01);
266+
expect(g1).beCloseToWithin(g2, 0.01);
267+
expect(b1).beCloseToWithin(b2, 0.01);
268+
expect(a1).beCloseToWithin(a2, 0.01);
269+
}
270+
#endif
271+
expect([topCenterColor.sd_hexString isEqualToString:tintColor.sd_hexString]).beTruthy();
249272
}
273+
#pragma clang diagnostic pop
250274

251275
- (void)test07UIImageTransformBlurCG {
252276
[self test07UIImageTransformBlurWithImage:self.testImageCG];
@@ -357,7 +381,7 @@ - (void)test09ImagePipelineTransformer {
357381
@"SDImageRoundCornerTransformer(50.000000,18446744073709551615,1.000000,#ff000000)",
358382
@"SDImageFlippingTransformer(1,1)",
359383
@"SDImageCroppingTransformer({0.000000,0.000000,50.000000,50.000000})",
360-
@"SDImageTintTransformer(#00000000,20)",
384+
@"SDImageTintTransformer(#00000000,18)",
361385
@"SDImageBlurTransformer(5.000000)",
362386
@"SDImageFilterTransformer(CIColorInvert)"
363387
];

0 commit comments

Comments
 (0)