IOS image stretching and ios stretching

Source: Internet
Author: User

IOS image stretching and ios stretching

Assume that the following image is used as the background image of the button. The original size is 76 Gb/s × 40

We use the code to set this image as the background image of the button. If we set the width and height of the created button :(W=200, H=50) The Code is as follows:

// Initialize the button UIButton * button = [[UIButton alloc] init]; // set the size of the button. frame = CGRectMake (100,200,200, 50); // load the image UIImage * image = [UIImage imageNamed: @ "ppm_new_shuliang.png"]; // set the background image [button setBackgroundImage: image forState: UIControlStateNormal]; // Add button [self. view addSubview: button];

The result is as follows: The image is stretched.

 

Cause analysis: the original size of the image was 76 Tib × 40 was stretched into W = 200, H = 50;
Solution:
1. Find an artist and redo a large image. In this way, the software package will become larger in the future, occupying more space. If we need to modify the frame of the button frequently, the design of the artist is complicated;
2. Apple provides us with APIs for image stretching, which can be implemented directly using code;

After modification:

// Initialize the button UIButton * button = [[UIButton alloc] init]; // set the size of the button. frame = CGRectMake (100,200,200, 50); CGFloat top = 0; // top cover height CGFloat bottom = 0; // bottom end cover height CGFloat left = 22; // left End Cover width CGFloat right = 22; // right End Cover width UIEdgeInsets insets = UIEdgeInsetsMake (top, left, bottom, right ); // load image UIImage * image = [UIImage imageNamed: @ "ppm_new_shuliang.png"]; image = [image resizableImageWithCapInsets: insets resizingMode: Custom]; // set the background image [button setBackgroundImage: image forState: UIControlStateNormal]; // Add button [self. view addSubview: button];

 

 

 

Another setting method is as follows:

 

 

 

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.