1. Select Stretch Uiimageview *fieldimage=[[uiimageviewalloc]initwithframe:cgrectmake (37,48+35,240, 32)]; Fieldimage.userinteractionenabled=yes;fieldimage.contentstretch=cgrectmake (0,0.4, 1, 0.5); fieldImage.image=[ uiimageimagenamed:@ "input box"]; [Self.viewaddSubview:fieldImage]; 2. Picture stretching skills throughout the mobile market, a mobile app, to long-term in the 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: This button: button.png, Size: 24x60
Now we use it as the button background, the button size is 150x50://get the view size cgsize viewsize = self.view.bounds.size;//Initialize button UIButton *button = [[UIButton All OC] init]; Set Dimensions button.bounds = CGRectMake (0, 0, 150, 50); Set Position Button.center = Cgpointmake (Viewsize.width * 0.5f, Viewsize.height * 0.5f); Load picture UIImage *image = [UIImage imagenamed:@ "button"]; Set background Image [Button setbackgroundimage:image Forstate:uicontrolstatenormal]; Add button [Self.view Addsubview:button]; Run: You can see that 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, iOS 5.0 has a concept called end caps (end caps) 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 cover Width-(UIImage *) Stretchableimagewithleftcapwidth: (Nsinteger) Leftcapwidth topcapheight: (Nsinteger) Topcapheight; This method has only 2 parameters, Leftcapwidth represents the left cover width, and topcapheight represents the top cover height. The system will automatically calculate the right end cover width (rightcapwidth) and the bottom cover height (bottomcapwidth), the algorithm is as follows://width for picture width rightcapwidth = width-leftcapwidth-1; Height topcapheight = height-topcapheight-1 after calculation, you will find that the middle stretch area is only 1x1//stretchwidth the width of the middle stretchable area Stretchwidth = wid Th-leftcapwidth-rightcapwidth = 1; Stretchheight is the height of the middle stretchable area stretchheight = Height-topcapheight-bottomcapheight = 1; Therefore, using this method only stretches the area of 1x1 in the middle of the picture, does not affect the edges and corners. The following shows the use of the method://Left cover width nsinteger leftcapwidth = image.size.width * 0.5f; Top cover Height Nsinteger topcapheight = image.size.height * 0.5f; Re-assign image = [image stretchableimagewithleftcapwidth:leftcapwidth Topcapheight:topcapheight]; After calling this method, The original image does not change, resulting in a new stretched uiimage, so the 6th line needs to assign the return value back to the image variable to run the effect:
Can be found that the picture is very beautiful to show out the attention:1. This method expires when iOS 5.0 comes out .2. This method only stretches the 1x1 area.Second, IOS 5.0
iOS stretched bottom cover settings