Simple image stretching and simple image stretching

Source: Internet
Author: User

Simple image stretching and simple image stretching

The initial image size is 25*28 pixels.

Now we try to create two buttons on the Root

 

UIButton * button = [UIButton buttonWithType: UIButtonTypeCustom];

Button. frame = CGRectMake (50,100, 25, 28 );

[Button setBackgroundImage: [UIImage imageNamed: @ "btn.png"] forState: UIControlStateNormal];

[Self. view addSubview: button];

UIButton * button1 = [UIButton buttonWithType: UIButtonTypeCustom];

Button1.frame = CGRectMake (50,200,200, 28 );

UIImage * image = [UIImage imageNamed: @ "btn.png"];

[Button1 setBackgroundImage: image forState: UIControlStateNormal];

[Self. view addSubview: button1];

Image effect before stretching

 

In iOS 5.0, there is a new way for UIImage to handle image stretching problems.

-(UIImage *) resizableImageWithCapInsets :( UIEdgeInsets) capInsets

CGFloat left = 12; // left end width

CGFloat right = 12; // right side width

UIEdgeInsets insets = UIEdgeInsetsMake (0, left, 0, right );

// Assign a value after scaling

UIImage * new = [image resizableImageWithCapInsets: insets];

[Button1 setBackgroundImage: new forState: UIControlStateNormal];

[Self. view addSubview: button1];

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.

 

CGFloat left = 12; // left end width

CGFloat right = 12; // right side width

UIEdgeInsets insets = UIEdgeInsetsMake (0, left, 0, right );

// Assign a value after scaling

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

[Button1 setBackgroundImage: new forState: UIControlStateNormal];

[Self. view addSubview: button1];

 

The following figure shows the stretched image.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.