UIImage
拉伸
代码
端盖
图片
方法1
// 我们输入的是左侧和顶部,会根据我们输入的自动计算右侧和底部的不可拉伸范围 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth // 指的是从左边边框到里面是多少不能拉伸的,直接填image.size.width*0.5 topCapHeight:(NSInteger)topCapHeight; // 指的是从顶部边框到里面是多少不能拉伸的,直接填image.size.height*0.5
// 如下计算之后,其实能拉伸的范围就是中间的1*1,就是上图的中间黑色部分 CGFloat rightCapWidth = width - leftCapWidth - 1; // width是image.width CGFloat bottomCapWidth = width - topCapWidth - 1; // stretchWidth为中间可拉伸区域的宽度 CGFloar stretchWidth = width - leftCapWidth - rightCapWidth = 1; // stretchHeight为中间可拉伸区域的高度 CGFloat stretchHeight = height - topCapHeight - bottomCapHeight = 1;
方法2
// 直接设置四个端盖的大小 - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets; // 上、左、下、右的端盖大小 UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
方法3添加了一个拉伸方法的属性
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
方法4设置图片
- 将图片拖到Images.xcassets,选中图片,点击slicing如下图:
- 自动计算需要保护的大小如下图:
- 将图片拖到Images.xcassets,选中图片,点击slicing如下图: