Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to apply a tintColor to a UIImage?

I have a UIImage that is a small symbol that is all black. The UIImage is getting set in a custom UIButton subclass I have. Is it possible to have the image to apply the tintColor to it, so instead of the black image it changes colors to whatever the tintColor is?

I'm just trying to avoid creating new assets.

// here I want defaultImageName (that is black) to use the tintColor (that is white) [self setImage:[UIImage imageNamed:defaultImageName] forState:UIControlStateNormal]; 
like image 532
aahrens Avatar asked Nov 06 '13 02:11

aahrens


People also ask

How do I change the UIImage color in Objective C?

setBlendMode(. multiply) ctx. draw(image. cgImage!, in: imageRect) let newImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return newImage! } }

How do you add a UIImage in Objective C?

UIImage *img = [[UIImage alloc] init]; [img setImage:[UIImage imageNamed:@"anyImageName"]];


1 Answers

If you are just supporting iOS 7 you can use tintColor and UIImageRenderingModeAlwaysTemplate

This article covers that:

https://www.captechconsulting.com/blogs/ios-7-tutorial-series-tint-color-and-easy-app-theming

If you need to support an earlier version you may want to consider this thread

How would I tint an image programmatically on the iPhone?

like image 92
madmik3 Avatar answered Sep 19 '22 06:09

madmik3