IPhone image stretching: resizableimagewithcapinsets

Source: Internet
Author: User

Http://blog.csdn.net/lixing333/article/details/7589281

Today, I made a thermometer application and needed a graph to stretch the red image in the thermometer based on the input data. To achieve this, the ios5 function is used: resizableimagecapinsets :( uiedgeinsets) insets.

The insets parameter is in the format of (top, left, bottom, right). A line is drawn from the top, left, bottom, and right, in this way, a box is added to an image. Only the part in the box is stretched, and the part outside the box is not changed. For example (20, 5, 10, 5), it means that the part in the rectangle can be stretched, while the rest remains unchanged.

It is said that the function stretchableimagewithleftcapwidth: topcapheight can also be implemented, but it is not recommended to use this function in ios5. The effect is as follows:

After the data is modified, it becomes as follows:

The following describes how to implement it.

A thermometer consists of three diagrams:

Background Image thermometerbackground.png:

Scale chart thermometercalibration:

The solution calibration:

First, add the background image to the superview, and then add the scale chart and solution diagram to the background image: (for simplicity, unnecessary code has been omitted)

[Plain]View
Plaincopy

  1. // Add the background image to the superview
  2. Uiimageview * thermometerbackground = [[uiimageview alloc] initwithframe: thermometer_frame];
  3. [Thermometerbackground setimage: [uiimage imagenamed: @ "thermometerbackground.png"];
  4. [Self. View addsubview: Self. thermometerbackground];
  5. // Add the solution diagram to the background image
  6. Uiimageview * thermometer = [[uiimageview alloc] init];
  7. [Self. thermometerbackground addsubview: Self. Thermometer];
  8. // Add the scale chart to the background image
  9. Uiimageview * thermometercalibration = [[uiimageview alloc] initwithimage: [uiimage imagenamed: @ "thermometercalibration.png"];
  10. [Self. thermometercalibration setframe: cgrectmake (0, 10, thermometerbackground. frame. size. width, thermometercalibration. image. size. height * thermometerbackground. frame. size. width/thermometercalibration. frame. size. width)];
[Plain]View
Plaincopy

  1. [Self. thermometerbackground addsubview: thermometercalibration];

Then, an image of the corresponding height is generated based on the degree;

[Plain]View
Plaincopy

  1. Uiimage * image = [uiimage imagenamed: @ "thermometer.png"];
  2. Uiedgeinsets insets = uiedgeinsetsmake (20, 0, 25, 0 );
  3. Image = [Image resizableimagewithcapinsets: insets];
  4. Int Top = 10.00 + (38.00-temperature) * 20.00;
  5. [Self. Thermometer setframe: cgrectmake (0, top, self. thermometerbackground. Frame. Size. Width, self. thermometerbackground. Frame. Size. Height-top)];
[Plain]View
Plaincopy

  1. [Self. Thermometer setimage: Image];

Here, the top variable represents the height of the solution calculated based on the degree.

In this way, when the temperature is changed, you can dynamically generate a thermometer image by calling this code in viewwillappear.

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.