IOS image stretching and ios stretching skills

Source: Internet
Author: User

IOS image stretching and ios stretching skills
IOS image stretching skills

Contents

  • I. Before iOS 5.0
  • Ii. iOS 5.0
  • Iii. iOS 6.0

Throughout the mobile market, a mobile app, if you want to establish a foothold in the mobile market for a long time, must at least include the following elements: practical functions, strong user experience, gorgeous and concise appearance. Behind the gorgeous appearance is the painstaking design of the artist. However, if developers do not know how to properly display these well-designed pictures, these designs will be ruined and will be at a loss.

For example, the following image was originally designed as a button Background:

Button.png, 24x60

Now we use it as the button background. The button size is 150x50:

1-(void) viewDidLoad 2 {3 [super viewDidLoad]; 4 // obtain the view Size 5 CGSize viewSize = self. view. bounds. size; 6 7 // initialization button 8 UIButton * button = [[UIButton alloc] init]; 9 // set the size of 10 buttons. bounds = CGRectMake (0, 0,150, 50); 11 // you can specify the position 12 button. center = CGPointMake (viewSize. width * 0.5f, viewSize. height * 0.5f); 13 14 // load image 15 UIImage * image = [UIImage imageNamed: @ "button"]; 16 // set the background image 17 [button setBackgroundImage: image forState: UIControlStateNormal]; 18 19 // Add button 20 [self. view addSubview: button]; 21}

Run:

We can see that the effect is very poor. The reason is very simple, because the source image size is 24x60, the whole image is now fully stretched to 150x50, the more serious is the four corners of the image.

Some people may come up with a solution immediately. Isn't it okay if you ask the artist to make the picture bigger? How to stretch it. Yes, this is a solution, but it is not recommended. The reason is very simple: 1. Large images, resulting in a large installation package and large loading to the memory; 2. A better solution.

Looking at a piece of film, the image will become ugly, because the four corners are stretched, and the middle stretch is not obviously ugly. Therefore, if you want a small image to be stretched, it will not become ugly. When you stretch the image, you only need to stretch the rectangular area in the middle of the image, instead of stretching the edge.

For example, the upper and lower left edges of a rectangular area are not stretched:

IOS provides an easy-to-use API to help us implement the above functions. As of iOS 6.0, iOS provides three image stretching solutions. Next, we will introduce these solutions in detail.

Back to Top I. Before iOS 5.0

There is an end cap in iOS that specifies which part of the image does not need to be stretched. For example, black indicates the area of the rectangle to be stretched. The top, bottom, left, and right edges that do not need to be stretched are called end caps.

Using this method of UIImage, you can set the End Cover width to return a stretched UIImage object.

1 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

This method has only two parameters. leftCapWidth indicates the width of the Left end cover, and topCapHeight indicates the height of the top cover. The system automatically calculates the right end cover width and bottom end cover height. The algorithm is as follows:

1 // width indicates the image width 2 rightCapWidth = width-leftCapWidth-1; 3 4 // height indicates the Image height 5 bottoffapheight = height-topCapHeight-1

After calculation, you will find that the middle stretch area is only 1x1

1 // stretchWidth is the width of the middle stretch area 2 stretchWidth = width-leftCapWidth-rightCapWidth = 1; 3 4 // stretchHeight is the height of the middle stretch area 5 stretchHeight = height-topCapHeight-botw.apheight = 1;

Therefore, using this method will only stretch the area 1x1 in the middle of the image, and will not affect the edge and corner.

The following shows how to use this method:

1 // width of the Left end cover 2 NSInteger leftCapWidth = image. size. width * 0.5f; 3 // top cover height 4 NSInteger topCapHeight = image. size. height * 0.5f; 5 // assign a value of 6 image = [image stretchableImageWithLeftCapWidth: leftCapWidth topCapHeight: topCapHeight];

After this method is called, the original image will not change and a new stretched UIImage will be generated. Therefore, the value of the returned value must be assigned back to the image variable in line 1.

Running effect:

You can find that the image is displayed beautifully.

Note:

1. This method expired after iOS 5.0 came out.

2. This method can only stretch the area of 1x1.

 

Back to Top Ii. iOS 5.0

In iOS 5.0, another new method of UIImage processing is available for image stretching.

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

This method only receives one UIEdgeInsets parameter. You can set left, right, top, and bottom of UIEdgeInsets to specify the width, width, height, and height of the End Cover.

1 CGFloat top = 25; // top cover height 2 CGFloat bottom = 25; // bottom Cover height 3 CGFloat left = 10; // left End Cover width 4 CGFloat right = 10; // right End Cover width 5 UIEdgeInsets insets = UIEdgeInsetsMake (top, left, bottom, right); 6 // value 7 image = [image resizableImageWithCapInsets: insets] after scaling;

Running effect:

 

Back to Top Iii. iOS 6.0

In iOS6.0, UIImage provides another method for processing image stretching.

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode

Compared with iOS5.0, only one UIImageResizingMode parameter is added to specify the stretch mode:

  • UIImageResizingModeStretch: stretch mode. Fill the image by stretching the area of the rectangle specified by UIEdgeInsets.
  • UIImageResizingModeTile: tiled mode. The image is filled by repeatedly displaying the rectangle area specified by UIEdgeInsets.
1 CGFloat top = 25; // top cover height 2 CGFloat bottom = 25; // bottom Cover height 3 CGFloat left = 10; // left End Cover width 4 CGFloat right = 10; // right End Cover width 5 UIEdgeInsets insets = UIEdgeInsetsMake (top, left, bottom, right); 6 // specify the stretch mode and assign a value of 7 image = [image resizableImageWithCapInsets after scaling: insets resizingMode: UIImageResizingModeStretch];

Running effect:

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.