iOS图片拉伸:resizableImageWithCapInsets

移动开发 iOS
今天做了一个温度计的应用,需要一个图,能够根据输入的数据将温度计里面的红色图片拉伸。为了达到这个效果,使用了iOS5的函数:resizableImageCapInsets:(UIEdgeInsets)Insets。最近终于申请到苹果开发者账号!搞的好烦啊!给大家带福利了!

今天做了一个温度计的应用,需要一个图,能够根据输入的数据将温度计里面的红色图片拉伸。为了达到这个效果,使用了iOS5的函数:resizableImageCapInsets:(UIEdgeInsets)Insets。

最近终于申请到苹果开发者账号!搞的好烦啊!给大家带福利了!

想真机调试,上架应用,将IPA打包给朋友用,或者申请开发者账号的请联系我!

真机调试有99个限制!

其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片加了一个框。只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。比如(20,5,10,5),意思是下图矩形里面的部分可以被拉伸,而其余部分不变。

据说stretchableImageWithLeftCapWidth:topCapHeight这个函数也能够实现,但是在iOS5里面建议不要使用这个函数。效果如下图:

当修改了数据之后,变成这样:

下面来看如何实现。

温度计共由三张图组成:

背景图ThermometerBackground.png:

刻度图ThermometerCalibration:

里面的溶液Calibration:

首先将背景图加入superview中,再将刻度图和溶液图加入背景图中:(为简化起见,一些不必要的代码已经省略)

  1. //将背景图加入superview  
  2. UIImageView *thermometerBackground = [[UIImageView alloc] initWithFrame:THERMOMETER_FRAME];  
  3. [thermometerBackground setImage:[UIImage imageNamed:@"ThermometerBackground.png"]];  
  4. [self.view addSubview:self.thermometerBackground];  
  5. //将溶液图加入背景图  
  6. UIImageView *thermometer = [[UIImageView alloc]init];  
  7. [self.thermometerBackground addSubview:self.thermometer];  
  8. //将刻度图加入背景图  
  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];  

然后,根据度数生成对应高度的image

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

在这里,top这个变量就代表了根据度数计算出的溶液的高度。

这样,当改变温度temperature的大小时,只要在viewWillAppear里调用这段代码,就能够动态生成温度计图片了。

责任编辑:闫佳明 来源: apkbus
相关推荐

2013-08-21 11:31:21

iPhone图片方法

2021-12-29 06:24:16

AI审稿人工智能

2015-09-09 11:08:48

qq空间可拉伸头部

2015-01-20 17:15:55

iOS源码滚动视图

2015-08-10 09:50:21

ios图片文本

2018-05-16 07:41:29

图片代码资源

2023-04-27 08:42:50

效果

2022-05-26 00:06:19

CSSFirefoxElectron

2023-04-25 17:24:31

veImageXiOSSDK

2011-09-19 15:42:33

TwitteriOS5

2010-07-26 15:12:20

坐标变换

2013-07-29 11:19:16

iOS开发iOS开发学习FMDB更新二进制图片

2011-05-12 11:28:20

按比例缩放

2013-10-16 16:58:17

iOS优化缓存优化

2015-04-23 10:15:53

AndroidiOS图片

2011-06-02 10:12:54

TwitteriOS 5苹果

2015-04-23 10:52:53

AndroidiOS图片

2011-08-18 17:20:21

IOS开发TableView图片

2018-03-15 15:09:29

iOS图片标记苹果

2013-10-16 16:17:15

iOS开发优化方案
点赞
收藏

51CTO技术栈公众号