方式一:
具体介绍:
// /<#(UIEdgeInsets)#> 设置一个内边距 这个方法,根据内边距 上左下右 画四条线
// 四条线有一个相交的区域<
// 在设置的时候为了保证四周的一个区域不进行拉伸
// 方式 上下左右的内边距各设置为控件宽度的一半
// UIImageResizingModeTile, 瓦片的
// UIImageResizingModeStretch, 直接拉伸
使用方式:
//1)先定义要拉伸的图片
UIImage *meImage = [UIImage imageNamed:@"chat_send_nor"];
UIImage *lastmeImage= [meImage resizableImageWithCapInsets:UIEdgeInsetsMake(meImage.size.height * 0.5, meImage.size.width * 0.5, meImage.size.height * 0.5, meImage.size.width * 0.5) resizingMode:UIImageResizingModeStretch];
[self.messageBtn setBackgroundImage:lastmeImage forState:UIControlStateNormal];
具体介绍:
//图片拉伸的第二种方式,它默认就是 直接拉伸 的方式
// WithLeftCapWidth 左边对应宽
// topCapHeight 上面对应高
// default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1
// 这个方法只需要设置左边和上面,右边和下面由系统计算
// 右边 =width - leftCapWidth - 1
// 下面 = height -topCapHeight - 1
// 这个方法,比上面的方法出现的要早 -->后来发现有一些图片(方格的图片)不适合直接拉伸->所以由增加了一种瓦片的拉伸方式
使用方式:
UIImage *imageOther = [UIImage imageNamed:@"chat_recive_nor"];
UIImage *lastImageOther = [imageOther stretchableImageWithLeftCapWidth:imageOther.size.width * 0.5 topCapHeight:imageOther.size.height * 0.5];
//默认状态
[self.messageBtn setBackgroundImage:lastImageOther forState:UIControlStateNormal];