ios菜鸟之路:图片的拉伸的2种方法
图片拉伸,多用于聊天中的气泡,随着字体的增多而变高变宽。但是要保证边角的不变,只拉伸中间的部分。用到2种方法:
1、(UIImage *)stretchableImageWithLeftCapWidth:(NSInterger) topCapHeight:(NSinterger)
这种方法只适用于ios5以下的版本。它只无限拉伸离最左边像素(第一个参数值) +1和离最上边的像素的值+1(第二个参数值)。
2、(UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)
//方法介绍
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode
//参数结构
typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;
//用法示例
UIImageView *ImageView=[[UIImageView alloc]init];
[ ImageView setFrame:CGRectMake(126.0, 80.2, 30.0, 20.0)];
UIEdgeInsets ed = {0.0f, 10.0f, 0.0f, 10.0f};
[ImageView setImage:[[UIImage imageNamed:@"xxx.png"]resizableImageWithCapInsets:ed]];
self.view addSubview:ImageView ];
//以上左10.0,右10.0,是左边10像素以内右边10像素以内不拉伸,中间拉伸。如果上下左右都有参数,就说明九宫格的四个角不拉伸,其他都拉伸
相关新闻>>
- 发表评论
-
- 最新评论 更多>>