IOS-modification method for rendering the image into blue
In the previous development process, we encountered a small problem: setting an image for the button, the image is gray, and the diagram made by the artist is certainly no problem, the method for setting an image for the button is also very simple. It can be done with a piece of code, but it is found that the image turns blue for no reason. Later, I made a research and found that when setting images for some controls, the image will be rendered in blue by default, and the problem will be found. The solution is as follows:
UIButton *loginBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)]; loginBtn.showsTouchWhenHighlighted = YES; UIImage *loginImg = [UIImage imageNamed:@"login"]; loginImg = [loginImg imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; [loginBtn setImage:loginImg forState:UIControlStateNormal]; [self.view addSubview:loginBtn];
-(UIImage *) imageWithRenderingMode :( UIImageRenderingMode) renderingMode
This method is used to set the rendering mode of the image.
The enumerated value of UIImageRenderingModeAlwaysOriginal indicates that the image will be displayed in the original format without rendering it into other colors.
OK! The problem has ended. Record this small problem here to prevent you from forgetting how to solve it next time!
By the way, if your artist does not give you the size of the button, but just gives you the image of the button, you can get the size of the image directly, set the button to the same size as the image.
UIButton * loginBtn = [UIButton buttonWithType: UIButtonTypeCustom]; loginBtn. required = YES; UIImage * loginImg = [UIImage imageNamed: @ "login"]; loginImg = [loginImg imageWithRenderingMode: enabled]; [loginBtn setImage: loginImg forState: enabled]; CGSize size = loginBtn. currentImage. size; // obtain the loginBtn size of the image of the button. frame = CGRectMake (100,100, size. width, size. height); [self. view addSubview: loginBtn];