1 [[UIImage imageNamed:@"button_textured_30"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 5, 0, 5)];

其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片加了一个框。

只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。

4个参数是上边界,左边界,下边界,右边界距离,也可以为负值。

其中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里调用这段代码,就能够动态生成温度计图片了。

iPhone图片拉伸:resizableImageWithCapInsets的更多相关文章

  1. iOS 图片拉伸 resizableImageWithCapInsets

    UIImage *image =  [[UIImage imageNamed:@"test.png"] resizableImageWithCapInsets:UIEdgeInse ...

  2. 使用图片拉伸resizableImageWithCapInsets

    在仿写QQ会话的时候背景蓝色图片是拉伸而来,但是有些地方是受保护的不能拉伸 所以定义了下面的工具类中的一个方法,专门拉伸图片 UIImageResizingModeStretch:拉伸模式,通过拉伸U ...

  3. ios UIImage图片拉伸 resizableImageWithCapInsets:

    常见的按钮添加和背景设置如下: UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 130, 160, 44)];[bu ...

  4. iOS图片拉伸技巧

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...

  5. 【原】iOS学习之图片拉伸处理(类似qq的气泡)

    原理是拉伸里面的内容,将边保护起来 方法1: ①[image resizableImageWithCapInsets:UIEdgeInsetsMake(, , , )]; ②[image resiza ...

  6. iOS开发——UI基础-按钮内边距,图片拉伸

    一.内边距 UIButton有三个属性,分别可以设置按钮以及内部子控件的内边距 1.contentEdgeInsets 如果是设置contentEdgeInsets, 会把UIImageView和UI ...

  7. (转)iOS图片拉伸技巧

    ( 原文博客地址:  http://blog.csdn.net/q199109106q/article/details/8615661) 纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要 ...

  8. IOS中图片拉伸技巧与方法总结(转载)

    以下内容转载自:http://my.oschina.net/u/2340880/blog/403996 IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是 ...

  9. iOS杂谈-图片拉伸的实现

    如上图是一个按钮的背景图,在Android上,很多图片资源都是类似这样子的,但是由于按钮的高度及宽度与图片的世纪尺寸不同,所以需要采用9patch来实现拉伸处理, 可参考:http://www.cnb ...

随机推荐

  1. 微信小程序获取验证码倒计时

    getVerificationCode: function() { var that = this; var currentTime = that.data.currentTime; that.set ...

  2. MyEclipse激活失败

    最近从MyEclipse2014升级MyEclipse2015,结果按照MyEclipse2014的方式激活2015总是失败,显示错误如下图所示: 反复实验,怎么也不能成功激活,最终找到方法 很多情况 ...

  3. Python3.x:定时自动发送邮件

    定时自动发送邮件 一.简述 python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是python自带的,只需import即可使用.smtplib模块主要负责发送邮件,email ...

  4. Vim:gvim安装配置(windows)

    Vim:gvim安装配置(windows) 一.gvim的特点: vim要求全部键盘操作,而gvim可以使用鼠标进行可视化操作,即gvim是vim的图形化界面: 二.gvim安装: 下载地址:http ...

  5. 20145235李涛《网络对抗》Exp2 后门原理与实践

    Windows获得Linux Shell Linux获得windows shell 实验内容 使用netcat获取主机操作shell,cron启动 使用socat获取主机shell,任务计划启动 使用 ...

  6. React Native常用组件之TabBarIOS、TabBarIOS.Item组件、Navigator组件、NavigatorIOS组件、React Navigation第三方

    以下内容为老版本React Native,faceBook已经有了新的导航组件,请移步其他博客参考>>[我是传送门] 参考资料:React Navigation  react-native ...

  7. Mysql 基本用法

    Java中两种常用的数据库: MYSQL     Oracle MYSQL  :开源免费的数据库,小型的数据库.由瑞典MySQL AB 公司开发,适合中小企业使用,由C语言和C++编写的.已经被Ora ...

  8. Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).

    Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). Git fet ...

  9. Pandas统计函数

    统计方法有助于理解和分析数据的行为.现在我们将学习一些统计函数,可以将这些函数应用到Pandas的对象上. pct_change()函数 系列,DatFrames和Panel都有pct_change( ...

  10. glance cache

    用在多个glance API server 中,对相同的image文件提供服务.该cache对用户透明. 配置文件有一个image_cache_max_size,超过的话image cache会被修剪 ...