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: