转自: http://blog.csdn.net/phunxm/article/details/42150469
For example, you can use this method to create a background image for a button withborders andcorners: when the button is resized, the corners of the image remain unchanged, but the borders and center of the image expand to cover the new size.
当我们在创建变宽按钮时,往往保持按钮的圆角,只是让中心部分进行必要地伸缩。
例如,你可以使用stretchable或resizable来设置圆角按钮的背景贴图,当按钮尺寸发生改变时,背景贴图的圆角保持不变,中心部分进行必要伸缩来适应新的尺寸。
specifying cap insets such that the interior is a 1x1 area.
During scaling or resizing of the image, areas covered by a cap are not scaled or resized.
Instead, the 1-pixel wide area not covered by the cap in each direction is what is scaled or resized.
@property(nonatomic,readonly) NSInteger leftCapWidth:horiz. stretchable. 左边不拉伸区域的宽度。
@property(nonatomic,readonly) NSInteger topCapHeight:vert. stretchable. 上面不拉伸区域的高度。
根据UIKit/UIImage.h对该属性的描述
UIEdgeInsets capInsets = UIEdgeInsetsMake(topCapHeight,leftCapWidth,bottomCapHeight,rightCapWidth)
因此,实际stretchable的interior area是capInsets封盖圈起来的部分——一个1x1点阵。也即坐标为{left+1,top+1}的这个点(one point)会像贴瓷砖那样进行复制(UIImageResizingModeTile:the interior is tiled when drawn),capInsets封盖的周边外围保持不变。
为视效均匀起见,一般选取图片的中心点(leftCapWidth=width/2,topCapHeight=height/2)进行复制填充。 // create a resizable version of this image. the interior is tiled when drawn.
@property(nonatomic,readonly)UIEdgeInsetscapInsets;
NS_AVAILABLE_IOS(5_0)
【Summary】For best performance, use a tiled area that is a 1x1 pixel area in size.
iOS uses different rendering techniques, with differentperformance characteristics, depending on the size of each resizable area in the image:
选取图片的中心点(height/2,width/2,height/2-1,width/2-1)进行复制填充时,效果同stretchableImageWithLeftCapWidth:width/2 topCapHeight:height/2,对1x1 pixel area进行拉伸(stretching)适配。对于纯色图片,选取中心点拉伸效能最优。
若capInsets封盖部分的区域大于1x1,则对尺寸超出部分,像贴瓷砖一样复制填充(tile/spread/extend)。对于纹理贴图(textured),通常采用此种方式。
// the interior is resized according to the resizingMode
This method is exactly the same as its counterpart resizableImageWithCapInsets:except that the resizing mode of the new image object can be explicitly declared.
You should only call this method in place of its counterpart if you specifically want your image to be resized with the UIImageResizingModeStretch resizing mode.
【解说】 resizableImageWithCapInsets: resizingMode:UIImageResizingModeTile相当于resizableImageWithCapInsets: ,一般直接调用resizableImageWithCapInsets:。
纵向拉伸:UIEdgeInsetsMake(coreRadius, 0, coreRadius, 0),例如类似温度计的圆角/纵向胶囊按钮贴图。
半角拉伸:UIEdgeInsetsMake(coreRadius, coreRadius,coreRadius, coreRadius),例如带corner的圆角登录按钮。
例如,我们要用贴图[email protected](39x20)和贴图[email protected](39x20) 实现如下贴图效果:
其中分割线上半部由[email protected]贴出顶部圆角效果,分割线下半部由[email protected]贴出底部圆角效果。其拉伸属性设置代码如下:
最后,给出一个关于UIImageView和UIButton贴图的综合示例(fan2/resizableImage):
(1)第一组:上边一个左上、右上带圆角贴图,下边一个左下、右下带圆角贴图。
(2)第二组:登录按钮,四角圆角贴图。
(3)第三组:微信聊天气泡贴图效果。
(4)第四组:横向胶囊圆角按钮贴图示例:
- 初始胶囊按钮,背景为蓝圈白底,字体为蓝色。
- 左右按钮初始图标分别为大拇指竖起和大拇指向下。
- 选择左胶囊按钮,背景变蓝,字体变白,图标由大拇指竖起变为一朵鲜花。
- 选择右胶囊按钮,背景变蓝,字体变白,图标由大拇指向下变为一朵枯萎。
以下为UIImageView和UIButton贴图的CapInsets标注:
参考:
《iOS图片拉伸技巧》
《[UIImage resizableImageWithCapInsets:]使用注意》
《iOS 不规则的ImageView》《iOS 实现聊天剪裁气泡》