UIImage change Backgroundcolor - ios

All,
We have icon design from PhotoShop, It is Square transparent image with a Polygon inside which has a blue color. Based on some conditions i want to change the Polygon color but on the complete image color. Can we do this in code (Objective C)
I am attaching a Image in which the Only background "blue" color need to be changed based on the color we give. How can we achevie that

You can do this by rendering the image in UIImageRenderingModeAlwaysTemplate mode & after that set tintColor.
imageView.image = [imageView.image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
[imageView setTintColor:[UIColor redColor]];

Related

Change UIImage base color, keep white

I was trying to use the method outlined here for changing the color of a UIImage used for a UIView. I was hoping this method would ignore whitespace but it does not.
Here is the original image in which I'd like to replace the red with blue.
Here is the code
UIImage *image = [UIImage imageNamed:#"star-badge"];
image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:...];
imageView.tintColor = [UIColor blueColor];
imageView.image = image;
[self.view addSubview:imageView];
but this is the result
is there a way to ignore the white space or only change the red color?
Change the original image so that the middle is transparent instead of white, then the method you're using should work. If need be, display it on a white background.
As David says, you need a transparent star, attached the image how should be just in case you cant handle it on photoshop

IOS Button border thickness lssue,why?

UIButton top border appears thicker than the following ,but sometimes correct ,why?
code:
UIImage * sanImage = [UIimage imageNamed:#"product_bt1_normal"];
[self.saveBtn setBackgroundImage:[sanImage
stretchableImageWithLeftCapWidth:sanImage.size.width/3
topCapHeight:sanImage.size.height/3] forState:UIControlStateNormal];
Are you trying to make a button? If so, perhaps use a UIButton instead? You can control the border with button.layer.borderWidth = 1.0f
If you're set on using an image, create a UIImageView, and modify the border thickness that way:
UIImageView *iv = [[UIImageView alloc] initWithImage:sanImage];
[iv.layer setBorderWidth:0.5f];
It could be because of off-pixel boundaries. Since you are using height/3.0f, your image is maybe not returning a well-behaved image.
Also, there is a new stretchable image method you should be using, resizableImageWithCapInsets:.
So try this code out:
[self.saveBtn setBackgroundImage:[sanImage resizableImageWithCapInsets:UIEdgeInsetsMake(3.0f, 3.0f, 3.0f, 3.0f)] forState:UIControlStateNormal];
You might need to mess with the values for the insets a bit, I don't know the dimensions of your button image.

Conflicting UIBarbuttonItem's tintcolor and image

I have a UIToolbar in one of my VCs, it has 3 color buttons which changes the color of my drawing. Anyways I want to change the button's image when its selected. The images are shown below, the problem is apparently the button's "tintcolor" is messing with the original image.
If i set the "tintcolor" to red my active button looks like a bigger red circle, if its "clearcolor" it doesn't show. Any help would be much appreciated guys.
UIImage *image = [UIImage imageNamed:#"red-selected"];
[button setImage:image];
I even tried:
UIImage *image = [[UIImage imageNamed:#"red-selected"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[btn setImage:#"red-selected.png" forState:UIControlStateNormal];
you can also do it in the interface builder - indicates a photo for selected mode.
pay attention the type of the photo- is it png?
try #"red-selected.png"/ #"red-selected.jpg"

How to change tint color of the UNSELECTED pictures in a tabBar

If I do self.tabBar.tintColor = [UIColor whiteColor];
I manage to get the image of the selected tab bar white.
How do I get the image of the unselected tab bar black, or dark grey or red?
You can use something like this. The clue of this line of code is UIImageRenderingModeAlwaysOriginal. That means that the code is showing the original image. If your image is red, the icon will be red and if your image is blue, your icon will be blue.
Add this code in the first ViewController for every TabBarItem
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarItem.image = [[UIImage imageNamed:#"yourImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.tabBarItem.selectedImage = [[UIImage imageNamed:#"yourImage.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
...
{
Now, you don't need your self.tabBar.tintColor = [UIColor whiteColor]; any more.
Rendering Modes by Apple Documentation:
UIImageRenderingModeAutomatic, // Use the default rendering mode for the context where the image is used
UIImageRenderingModeAlwaysOriginal, // Always draw the original image, without treating it as a template
UIImageRenderingModeAlwaysTemplate, // Always draw the image as a template image, ignoring its color information
Check this answer: stackoverflow.com/a/22766669/1381708

Custom UINavigationBar background image appears darker than the original image

I am customizing a UINavigationBar like so:
UIImage * img = [UIImage imageNamed:#"background.png"];
[[UINavigationBar appearance] setBackgroundImage:img forBarMetrics:UIBarMetricsDefault];
My original image background.png looks like this:
It has a flat color #4b0367
The resulting NavigationBar looks like this:
It has the color #311253 which is different from the original!
Apparently UIKit changes the color.
Note:
Setting tintColor had no effect.
Setting a transparent background image + a color is not an appropriate solution in my case.
Does anybody know how the underlying mechanism works?
How can I get the original image to appear correctly?

Resources