Re: Transparent Image with Tint Color
Re: Transparent Image with Tint Color
- Subject: Re: Transparent Image with Tint Color
- From: email@hidden
- Date: Mon, 30 Sep 2013 15:05:30 +0900
Sent from my iPhone
> On 2013/09/30, at 11:37, Paul Scott <email@hidden> wrote:
>
>
>> 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 this is unusual in Cocoa and Cocoa touch. I thought the same thing the first time.
These are actually Objective-C wrappers around Core Graphics (Quartz) functions.
They mainly make sense when you learn about how CG works and that these are actually setting parameters of the graphics state of the current graphics context in Quartz.
Unfortunately it's a winding road to learn and know this.
I recommend highly the dates but still VERY well explained book
Programming With Quartz: 2D and PDF Graphics in Mac OS X
>
> 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