diff --git a/Source/ASImageNode.mm b/Source/ASImageNode.mm index 79dc4282..63cadcf8 100644 --- a/Source/ASImageNode.mm +++ b/Source/ASImageNode.mm @@ -594,6 +594,14 @@ static ASWeakMap *cache = nil; [self setNeedsDisplay]; } +- (void)tintColorDidChange +{ + [super tintColorDidChange]; + if (_image.renderingMode == UIImageRenderingModeAlwaysTemplate) { + [self setNeedsDisplay]; + } +} + #pragma mark Interface State - (void)clearContents diff --git a/Source/Details/ASGraphicsContext.mm b/Source/Details/ASGraphicsContext.mm index 95db9226..38ec04dd 100644 --- a/Source/Details/ASGraphicsContext.mm +++ b/Source/Details/ASGraphicsContext.mm @@ -47,7 +47,15 @@ UIImage *ASGraphicsCreateImageWithOptions(CGSize size, BOOL opaque, CGFloat scal UIGraphicsImageRendererFormat *format; if (sourceImage) { - format = sourceImage.imageRendererFormat; + if (sourceImage.renderingMode == UIImageRenderingModeAlwaysTemplate) { + // Template images will be black and transparent, so if we use + // sourceImage.imageRenderFormat it will assume a grayscale color space. + // This is not good because a template image should be able to tint to any color, + // so we'll just use the default here. + format = defaultFormat; + } else { + format = sourceImage.imageRendererFormat; + } // We only want the private bits (color space and bits per component) from the image. // We have our own ideas about opacity and scale. format.opaque = opaque; diff --git a/Tests/ASImageNodeSnapshotTests.mm b/Tests/ASImageNodeSnapshotTests.mm index a31a7186..381064f2 100644 --- a/Tests/ASImageNodeSnapshotTests.mm +++ b/Tests/ASImageNodeSnapshotTests.mm @@ -30,6 +30,14 @@ return [UIImage imageWithContentsOfFile:path]; } +- (UIImage *)testGrayscaleImage +{ + NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"logo-square-black" + ofType:@"png" + inDirectory:@"TestResources"]; + return [UIImage imageWithContentsOfFile:path]; +} + - (void)testRenderLogoSquare { // trivial test case to ensure ASSnapshotTestCase works @@ -79,6 +87,25 @@ ASSnapshotVerifyNode(node, @"blue_tint"); } +- (void)testTintColorOnGrayscaleNodePropertyAlwaysTemplate +{ + ASConfiguration *config = [ASConfiguration new]; + config.experimentalFeatures = ASExperimentalDrawingGlobal; + [ASConfigurationManager test_resetWithConfiguration:config]; + + UIImage *test = [self testGrayscaleImage]; + ASImageNode *node = [[ASImageNode alloc] init]; + node.image = [test imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; + node.tintColor = UIColor.redColor; + ASDisplayNodeSizeToFitSize(node, test.size); + // Tint color should change view + ASSnapshotVerifyNode(node, @"red_tint"); + + node.tintColor = UIColor.blueColor; + // Tint color should change view + ASSnapshotVerifyNode(node, @"blue_tint"); +} + - (void)testTintColorOnNodePropertyAutomatic { UIImage *test = [self testImage]; diff --git a/Tests/ReferenceImages_iOS_10/ASImageNodeSnapshotTests/testTintColorOnGrayscaleNodePropertyAlwaysTemplate_blue_tint@2x.png b/Tests/ReferenceImages_iOS_10/ASImageNodeSnapshotTests/testTintColorOnGrayscaleNodePropertyAlwaysTemplate_blue_tint@2x.png new file mode 100644 index 00000000..2a1cc468 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASImageNodeSnapshotTests/testTintColorOnGrayscaleNodePropertyAlwaysTemplate_blue_tint@2x.png differ diff --git a/Tests/ReferenceImages_iOS_10/ASImageNodeSnapshotTests/testTintColorOnGrayscaleNodePropertyAlwaysTemplate_red_tint@2x.png b/Tests/ReferenceImages_iOS_10/ASImageNodeSnapshotTests/testTintColorOnGrayscaleNodePropertyAlwaysTemplate_red_tint@2x.png new file mode 100644 index 00000000..1d1b9d95 Binary files /dev/null and b/Tests/ReferenceImages_iOS_10/ASImageNodeSnapshotTests/testTintColorOnGrayscaleNodePropertyAlwaysTemplate_red_tint@2x.png differ diff --git a/Tests/TestResources/logo-square-black.png b/Tests/TestResources/logo-square-black.png new file mode 100644 index 00000000..af22c28d Binary files /dev/null and b/Tests/TestResources/logo-square-black.png differ