UIImage类中的常用拉伸方法
来源:互联网 发布:无印良品水乳 知乎 编辑:程序博客网 时间:2024/06/07 06:13
转载自: http://www.beauty-soft.net/blog/ceiba/Ios/20130516/641.html
一、resizableImageWithCapInsets
今天做了一个温度计的应用,需要一个图,能够根据输入的数据将温度计里面的红色图片拉伸。为了达到这个效果,使用了iOS5的函数:resizableImageCapInsets:(UIEdgeInsets)Insets。
其中Insets这个参数的格式是(top,left,bottom,right),从上、左、下、右分别在图片上画了一道线,这样就给一个图片加了一个框。只有在框里面的部分才会被拉伸,而框外面的部分则不会改变。比如(20,5,10,5),意思是下图矩形里面的部分可以被拉伸,而其余部分不变。
据说stretchableImageWithLeftCapWidth:topCapHeight这个函数也能够实现,但是在iOS5里面建议不要使用这个函数。效果如下图:
当修改了数据之后,变成这样:
下面来看如何实现。
温度计共由三张图组成:
背景图ThermometerBackground.png:
刻度图ThermometerCalibration:
里面的溶液Calibration:
首先将背景图加入superview中,再将刻度图和溶液图加入背景图中:(为简化起见,一些不必要的代码已经省略)
//将背景图加入superview
UIImageView *thermometerBackground = [[UIImageView alloc] initWithFrame:THERMOMETER_FRAME];
[thermometerBackground setImage:[UIImage imageNamed:@
"ThermometerBackground.png"
]];
[self.view addSubview:self.thermometerBackground];
//将溶液图加入背景图
UIImageView *thermometer = [[UIImageView alloc]init];
[self.thermometerBackground addSubview:self.thermometer];
//将刻度图加入背景图
UIImageView *thermometerCalibration = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@
"ThermometerCalibration.png"
]];
[self.thermometerCalibration setFrame:CGRectMake(
0
,
10
, thermometerBackground.frame.size.width, thermometerCalibration.image.size.height*thermometerBackground.frame.size.width/thermometerCalibration.frame.size.width)];
[self.thermometerBackground addSubview:thermometerCalibration];
然后,根据度数生成对应高度的image;
UIImage* image = [UIImage imageNamed:@
"Thermometer.png"
];
UIEdgeInsets insets = UIEdgeInsetsMake(
20
,
0
,
25
,
0
);
image = [image resizableImageWithCapInsets:insets];
int
top =
10.00
+(
38.00
-temperature)*
20.00
;
[self.thermometer setFrame:CGRectMake(
0
, top, self.thermometerBackground.frame.size.width, self.thermometerBackground.frame.size.height-top)];
[self.thermometer setImage:image];
在这里,top这个变量就代表了根据度数计算出的溶液的高度。
这样,当改变温度temperature的大小时,只要在viewWillAppear里调用这段代码,就能够动态生成温度计图片了。
二、stretchableImageWithLeftCapWidth
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:
(NSInteger)topCapHeight
这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是左边不拉伸区域的宽度,第二个参数是上面不拉伸的高度。根据设置的宽度和高度,将接下来的一个像素进行左右扩展和上下拉伸。
注意:可拉伸的范围都是距离leftCapWidth后的1竖排像素,和距离topCapHeight后的1横排像素。
参数的意义是,如果参数指定10,5。那么,图片左边10个像素,上边5个像素。不会被拉伸,x坐标为11和一个像素会被横向复制,y坐标为6的一个像素会被纵向复制。
注意:只是对一个像素进行复制到一定宽度。而图像后面的剩余像素也不会被拉伸。
UIImage *img=[UIImage imageNamed:@
"bubbleSelf.png"
];
img=[img stretchableImageWithLeftCapWidth:
15
topCapHeight:
12
];
UIImageView *imgView=[[UIImageView alloc]initWithImage:img];
[imgView setFrame:CGRectMake(
10
,
10
,
200
,
200
)];
[self. view addSubview:imgView];
- UIImage类中的常用拉伸方法
- UIImage类中的常用拉伸方法(转)
- UIImage 拉伸方法
- IOS uiimage 拉伸的方法
- UIImage属性及拉伸方法
- UIImage 拉伸特定的方法
- 拉伸UIImage
- UIImage拉伸
- iOS UIimage拉伸的三种方法
- iphone UIImage常用方法
- uiimage常用方法总结
- IOS图片拉伸的方法 UIImage stretchableImageWithLeftCapWidth:leftCapWidth:topCapHeight
- UIImage常用属性和方法
- iphone--UIImage图片拉伸
- UIImage等比拉伸
- UIImage 拉伸图片
- UIimage 图片的拉伸
- UIimage 的拉伸处理
- C#.ToString()格式大全
- weka实战005:基于HashSet实现的apriori关联规则算法
- codeforces 510c Fox And Names 拓扑排序
- Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
- 《C#高级编程》【第五章】泛型 -- 学习笔记
- UIImage类中的常用拉伸方法
- STL Vector整理
- 重新认识史蒂夫·乔布斯
- Swift流程控制
- 【Trie】【cogs647】有道搜索框
- 线程同步 资源锁定(一)
- Spring自带mock测试Controller
- 慧团网床上用品四件套家纺大全
- Nothing to be done for `_depend'.