IOS-modification method for rendering the image into blue

Source: Internet
Author: User

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];


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.