IOS image stretching: resizableImageWithCapInsets

Source: Internet
Author: User

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.

Recently, I finally applied for an Apple developer account! So annoying! Benefits for everyone!

If you want to debug a real machine, mount the application, package the IPA to a friend, or apply for a developer account, contact me!

Debugging on a real machine has 99 restrictions!

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 map to the background image. For simplicity, unnecessary code has been omitted)

 
 
  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)];
 
 
  1. [self.thermometerBackground addSubview:thermometerCalibration];  

Then, generate an image of the corresponding height based on the degree

 
 
  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)];  
 
 
  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.

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.