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:

  1. Button.png, Size: 24x60
  2. Now we use it as the button background, the button size is 150x50:
  3. (void) Viewdidload
  4. 2{
  5. 3[superviewdidload];
  6. 4//get the dimensions of the view
  7. 5cgsizeviewsize=self.view.bounds.size;
  8. 6
  9. 7//Initialize button
  10. 8uibutton*button=[[uibuttonalloc]init];
  11. 9//Setting Dimensions
  12. 10button.bounds=cgrectmake (0,0,150,50);
  13. 11//Setting Location
  14. 12button.center=cgpointmake (viewsize.width*0.5f,viewsize.height*0.5f);
  15. 13
  16. 14//Loading Pictures
  17. 15uiimage*image=[uiimageimagenamed:@ "button"];
  18. 16//setting a background image
  19. 17[buttonsetbackgroundimage:imageforstate:uicontrolstatenormal];
  20. 18
  21. 19//Add button
  22. 20[self.viewaddsubview:button];
  23. 21}

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. By iOS6.0, iOS offers 3 image stretching solutions, which are described in detail in the next step.

First, before iOS5.0

There is a concept called End caps (endcap) 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

  1. (uiimage*) Stretchableimagewithleftcapwidth: (Nsinteger) Leftcapwidthtopcapheight: (Nsinteger) topCapHeight;
  2. 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:
  3. 1//width for picture width
  4. 2rightcapwidth=width-leftcapwidth-1;
  5. 3
  6. 4//height for picture height
  7. 5bottomcapheight=height-topcapheight-1
  8. After calculation, you will find that the middle stretch area is only 1x1
  9. 1//stretchwidth is the width of the middle stretchable area
  10. 2stretchwidth=width-leftcapwidth-rightcapwidth=1;
  11. 3
  12. 4//stretchheight is the height of the middle stretchable area
  13. 5stretchheight=height-topcapheight-bottomcapheight=1;
  14. Therefore, using this method will only stretch the image in the middle of the 1x1 area, and will not affect the edges and corners.
  15. The following shows the use of the method:
  16. 1//Left cover width
  17. 2nsintegerleftcapwidth=image.size.width*0.5f;
  18. 3//Top Cover Height
  19. 4nsintegertopcapheight=image.size.height*0.5f;
  20. 5//re-assigning values
  21. 6image=[imagestretchableimagewithleftcapwidth:leftcapwidthtopcapheight: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 the iOS5.0 comes out.

2. This method only stretches the 1x1 area.

Second, iOS5.0

In iOS5.0, UIImage has a new way to deal with the drawing problem of the picture.

    1. -(uiimage*) Resizableimagewithcapinsets: (uiedgeinsets) capinsets
Copy Code

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

    1. 1cgfloattop=25;//Top Cover Height
    2. 2cgfloatbottom=25;//Bottom Cover Height
    3. 3cgfloatleft=10;//Left cover width
    4. 4cgfloatright=10;//right End cover width
    5. 5uiedgeinsetsinsets=uiedgeinsetsmake (Top,left,bottom,right);
    6. 6//scaling and re-assigning values
    7. 7image=[imageresizableimagewithcapinsets:insets];

Operating effect:


Third, iOS6.0

    1. In iOS6.0, UIImage also provides a way to process picture stretching
    2. -(uiimage*) Resizableimagewithcapinsets: (uiedgeinsets) Capinsetsresizingmode: (Uiimageresizingmode) ResizingMode
    3. Comparing the methods in iOS5.0, only one more Uiimageresizingmode parameter is used to specify the pattern of the extrusion:
    4. Uiimageresizingmodestretch: Stretch mode to fill a picture by stretching the rectangular area specified by the Uiedgeinsets
    5. Uiimageresizingmodetile: Tile mode, fills the picture by repeating the rectangular area specified uiedgeinsets
    6. 1cgfloattop=25;//Top Cover Height
    7. 2cgfloatbottom=25;//Bottom Cover Height
    8. 3cgfloatleft=10;//Left cover width
    9. 4cgfloatright=10;//right End cover width
    10. 5uiedgeinsetsinsets=uiedgeinsetsmake (Top,left,bottom,right);
    11. 6//specified as stretch mode, re-assigned after scaling
    12. 7image=[imageresizableimagewithcapinsets:insetsresizingmode:uiimageresizingmodestretch];
Copy Code

Operating effect:

iOS picture stretching tips

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.