IOS discussion-Implementation of image stretching

Source: Internet
Author: User

For a button background image, many image resources on Android are similar, but because the height and width of the button are different from the century size of the image, so we need to use 9patch to achieve Tensile Processing,

See: http://www.cnblogs.com/loulijun/archive/2011/12/22/2298087.html

The principle is to draw an area in the middle of the image through draw9patch. When the image is stretched, only the area is stretched, while other corners or areas are unchanged.

IOS can also implement this function, without the need for other tools to edit the image format, you only need to change it through code.

If the image is not processed, the result is as follows: The image is severely stretched.

The implementation code is as follows:

    CGSize viewSize =*button == CGRectMake(, , , = CGPointMake(viewSize.width * , viewSize.height * *image = [UIImage imageNamed:

The solution is to give a button-sized image resource, but the image size is too large, but the installation package is also large. In addition, by using a method similar to android, however, you do not need to convert it into a 9-patch image. You just need to set it in the code.

There is an end cap in iOS, which is used to specify which part of the image does not need to be stretched. The interior rectangular area is used for stretching and the periphery is unchanged, the top side of the rectangle is the top end cover (topCapHeight). The bottom is the bottom end cover (bottoapheight), the left side is the left End Cover (leftCapHeight), and the right side is the right end cover (rightCapHeight ), so in the end, only the internal rectangle is stretched, which will not cause image distortion.

After iOS5, UIImage has a method for processing image stretching.

-(UIImage *) resizableImageWithCapInsets :( UIEdgeInsets) capInsets

Accept a parameter of the UIEdgeInsets type, and set its left, right, top, and bottom to determine the width of the top, bottom, left, and right covers.

The Code is as follows:

    UIImage *image = [UIImage imageNamed:= = = = =    image = [image resizableImageWithCapInsets:insets];

In iOS6, another method is added.

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

The UIImageResizingMode parameter is used to specify the stretch mode.

1. UIImageResizingModeStretch: stretch mode. Fill the image by stretching the UIEdgeInsets to specify the hold

2. UIImageResizingModeTile: tiled mode. The image is filled by repeatedly displaying the UIEdgeInsets specified region.

The Code is as follows:

    UIImage *image = [UIImage imageNamed:= = = = =    image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];

Running effect:

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.