iOS picture stretching tips

Source: Internet
Author: User

Looking at the mobile market, a mobile app, in order to long-term mobile market foothold, at least to include the following elements: Practical features, a strong user experience, gorgeous and concise appearance. Gorgeous appearance behind, without the hard work of the design, but if developers do not know how to properly display these design good pictures, will spoil the design, fall short.

For example, the following picture, originally designed to do the button background:

Button.png, Size: 24x60

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

[Java]View Plaincopy
  1. Get the dimensions of the view
  2. Cgsize viewsize = self.view.bounds.size;
  3. Initialize button
  4. UIButton *button = [[UIButton alloc] init];
  5. Set Dimensions
  6. Button.bounds = CGRectMake (0, 0, 50);
  7. Set Location
  8. Button.center = Cgpointmake (Viewsize.width * 0.5f, viewsize.height * 0.5f);
  9. Loading pictures
  10. UIImage *image = [UIImage imagenamed:@"button"];
  11. Set a background picture
  12. [Button Setbackgroundimage:image Forstate:uicontrolstatenormal];
  13. Add button
  14. [Self.view Addsubview:button];

Run:

As you can see, the effect is very poor. The reason is very simple, because the original size is 24x60, and now the entire picture is stretched to 150x50, the more serious is the 4 corners of the picture.

Some people may immediately think of a solution, you call the artist to enlarge the picture is not good, how to stretch all right. Yes, it's a solution, but it's not recommended. The reason is simple: 1. The picture is large, resulting in the installation package is also large, loaded into memory is also large; 2. There is a better solution.

Look at a piece, in fact, the picture will become ugly, completely because the 4 corners are stretched, the middle of the stretching does not significantly smear the appearance. So to think that the small picture will not become ugly when stretched, when the picture is stretched, we just stretch the picture in the middle of a rectangular area, do not stretch the edge portion.

For example, only stretched rectangular areas, the upper and lower edges are not stretched:

iOS provides a very useful API to help us achieve the above features. To iOS 6.0, iOS offers 3 solutions for image stretching, which are described in more detail next.

First, before IOS 5.0

There is a concept called end caps in iOS that specifies which part of the picture does not have to be stretched. For example, the black represents the rectangular area that needs to be stretched, and the edges that do not need to be stretched up or down are called end caps.

Using this method of UIImage, you can return an extruded UIImage object by setting the end cap width

[Java]View Plaincopy
    1. -(UIImage *) Stretchableimagewithleftcapwidth: (Nsinteger) leftcapwidth topcapheight: (Nsinteger) topcapheight;

This method has only 2 parameters, the Leftcapwidth represents the left end cover width, and the topcapheight represents the top cover height. The system will automatically calculate the right end cover width (rightcapwidth) and the bottom cover height (bottomcapheight), the algorithm is as follows:

[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. width for picture widths
    2. Rightcapwidth = Width-leftcapwidth- 1;
    3. Height for picture Heights
    4. Bottomcapheight = height-topcapheight- 1

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

[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Stretchwidth is the width of the middle stretchable area
    2. Stretchwidth = Width-leftcapwidth-rightcapwidth = 1;
    3. Stretchheight is the height of the middle stretchable area
    4. Stretchheight = Height-topcapheight-bottomcapheight = 1;

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

The following shows the use of the method:

[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. Left cover width
    2. Nsinteger leftcapwidth = image.size.width * 0.5f;
    3. Top cover Height
    4. Nsinteger topcapheight = image.size.height * 0.5f;
    5. Re-assign Value
    6. image = [Image stretchableimagewithleftcapwidth:leftcapwidth Topcapheight:topcapheight];

When this method is called, the original image does not change, resulting in a new stretched uiimage, so the return value should be assigned back to the image variable in line 6th

Operating effect:

Can be found, the picture is very beautiful to show out

Attention:

1. This method expires when iOS 5.0 comes out.

2. This method only stretches the 1x1 area.

Second, IOS 5.0

In iOS 5.0, UIImage also has a new way to handle picture stretching issues

[Java]View Plaincopy
    1. -(UIImage *) Resizableimagewithcapinsets: (uiedgeinsets) capinsets

This method only receives a uiedgeinsets type parameter, which can be specified by setting the left, right, top and bottom of the uiedgeinsets, respectively, by specifying the width of the left cover, the width of the end cap, the top cover height, the bottom cover height

[Java]View Plaincopy
    1. CGFloat top = 25; //top cover height
    2. CGFloat bottom = 25; //Bottom cover height
    3. CGFloat left = 10; //Left cover width
    4. CGFloat right = 10; //Right End cover width
    5. Uiedgeinsets insets = Uiedgeinsetsmake (top, left, bottom, right);
    6. Re-assign value after scaling
    7. image = [Image resizableimagewithcapinsets:insets];

Operating effect:

Third, IOS 6.0

In iOS6.0, UIImage also provides a way to process picture stretching

[Java]View Plaincopy
    1. -(UIImage *) Resizableimagewithcapinsets: (uiedgeinsets) capinsets Resizingmode: (uiimageresizingmode) ResizingMode

Comparing the methods in iOS5.0, only one more Uiimageresizingmode parameter is used to specify the pattern of the extrusion:

    • Uiimageresizingmodestretch: Stretch mode to fill a picture by stretching the rectangular area specified by the Uiedgeinsets
    • Uiimageresizingmodetile: Tile mode, fills the picture by repeating the uiedgeinsets specified rectangular area

[Java]View Plaincopy < param name= "wmode" value= "Transparent" >
    1. CGFloat top = 25; //top cover height
    2. CGFloat bottom = 25; //Bottom cover height
    3. CGFloat left = 10; //Left cover width
    4. CGFloat right = 10; //Right End cover width
    5. Uiedgeinsets insets = Uiedgeinsetsmake (top, left, bottom, right);
    6. Specified as stretch mode, re-assigned after scaling
    7. image = [Image resizableimagewithcapinsets:insets Resizingmode:uiimageresizingmodestretch];

Operating effect:

Translated from: M A J Link: http://blog.csdn.net/q199109106q/article/details/8615661
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.