Re: Transparent Image with Tint Color
Re: Transparent Image with Tint Color
- Subject: Re: Transparent Image with Tint Color
- From: Paul Scott <email@hidden>
- Date: Sun, 29 Sep 2013 19:37:59 -0700
On Sep 29, 2013, at 6:54 PM, Kyle Sluder <email@hidden> wrote:
>
>> How do I get an image rendered in the tint color?
>
> Start a bitmap context, set your template image as the image mask, set the full color to the appropriate tintColor, and fill the bounds of the context.
>
> Construct a data URI out of the image you get by closing the bitmap context.
That works. Thanks. I was already heading down that path, since it seemed kind of obvious after thinking about it. Nevertheless, there are some strange and non-obvious dependencies that exist with various Cocoa and CG objects that aren't particularly well documented, such as [colorObject setFill]; I mean, what kind of magic happens there? Normally, I'd expect an instance method to operate on the receiver; but by this incantation the graphics context is involved.
Yes, I've got some learning ahead of me.
In any case, for anyone who cares, this code (in the webViewDidFinishLoad: method) worked:
UIImage *image = [controller.infoIcon.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIGraphicsBeginImageContext(image.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextSetBlendMode(context, kCGBlendModeNormal);
CGContextDrawImage(context, rect, image.CGImage);
CGContextSetBlendMode(context, kCGBlendModeColor);
[[webView tintColor] setFill];
CGContextFillRect(context, rect);
CGContextSetBlendMode(context, kCGBlendModeDestinationIn);
CGContextDrawImage(context, rect, image.CGImage);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *data = [UIImagePNGRepresentation(image) base64EncodedStringWithOptions:nil];
[webView stringByEvaluatingJavaScriptFromString:
[NSString stringWithFormat:@"%@%@%@",
@"( function() {"
@" var x = document.getElementById('infoIcon');"
@" if ( !! x ) { "
@" x.src = 'data:image/png;base64,", data, @"';"
@" }"
@"})();"
]
];
Paul
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden