From 4bd9ef29275f28f5b541d3cf101d9c32e248a8a7 Mon Sep 17 00:00:00 2001 From: andyscott Date: Tue, 3 Feb 2015 12:54:41 -0800 Subject: [PATCH] Add image modification block to render template images w/ tint. --- AsyncDisplayKit/ASImageNode.h | 12 ++++++++++++ AsyncDisplayKit/ASImageNode.mm | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/AsyncDisplayKit/ASImageNode.h b/AsyncDisplayKit/ASImageNode.h index c4b6686933..b04712bdb9 100644 --- a/AsyncDisplayKit/ASImageNode.h +++ b/AsyncDisplayKit/ASImageNode.h @@ -137,4 +137,16 @@ ASDISPLAYNODE_EXTERN_C_BEGIN */ asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock(CGFloat borderWidth, UIColor *borderColor); +/** + * @abstract Image modification block that applies a tint color à la UIImage configured with + * renderingMode set to UIImageRenderingModeAlwaysTemplate. + * + * @param tintColor The color to tint the image. + * + * @see + * + * @returns An ASImageNode image modification block. + */ +asimagenode_modification_block_t ASImageNodeTintColorModificationBlock(UIColor *color); + ASDISPLAYNODE_EXTERN_C_END diff --git a/AsyncDisplayKit/ASImageNode.mm b/AsyncDisplayKit/ASImageNode.mm index d8a7be58b4..bae53bc488 100644 --- a/AsyncDisplayKit/ASImageNode.mm +++ b/AsyncDisplayKit/ASImageNode.mm @@ -373,3 +373,20 @@ extern asimagenode_modification_block_t ASImageNodeRoundBorderModificationBlock( return modifiedImage; }; } + +extern asimagenode_modification_block_t ASImageNodeTintColorModificationBlock(UIColor *color) +{ + return ^(UIImage *originalImage) { + UIGraphicsBeginImageContextWithOptions(originalImage.size, NO, originalImage.scale); + + // Set color and render template + [color setFill]; + UIImage *templateImage = [originalImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate]; + [templateImage drawAtPoint:CGPointZero]; + + UIImage *modifiedImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return modifiedImage; + }; +} +