- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
这个方法是从iOS5开始的, 参数UIEdgeInset是指定拉伸区域用的, 也就是说被拉伸区域距离上下左右的间距, 方法的返回值是被拉伸后的图片,下面为应用示例
- (void)test { UIImage *iconView = [[UIImage alloc] init]; CGFloat top = iconView.size.width * 0.5; CGFloat left = iconView.size.height * 0.5; CGFloat bottom = iconView.size.width * 0.5; CGFloat right = iconView.size.height * 0.5; UIImage *newIconView = [iconView resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)]; }
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode此方法和第一个基本上一样, 多出一个参数拉伸模式的参数, 系统设定两种拉伸模式:第一种 UIImageResizingModeTile,瓦片拉伸, 也就是说用指定区域去平铺拉伸的空隙, 第二种 UIImageResizingModeStretch直接拉伸指定的区域, 应用示例如下:
- (void)test1 { UIImage *iconView = [[UIImage alloc] init]; CGFloat top = iconView.size.width * 0.5; CGFloat left = iconView.size.height * 0.5; CGFloat bottom = iconView.size.width * 0.5; CGFloat right = iconView.size.height * 0.5; UIImage *newIconView = [iconView resizableImageWithCapInsets:UIEdgeInsetsMake(top, left, bottom, right)resizingMode:UIImageResizingModeTile]; }第三个方法:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight __TVOS_PROHIBITED;这个方法和上面两个不太一样, 需要指定两个参数, topCapHeight和leftCapWidth; 实际上也是四个参数, 只不过下面的和右面的两个间距系统自动计算了. 两个参数的含义如下:
@property(nonatomic,readonly) NSInteger leftCapWidth __TVOS_PROHIBITED; // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1 @property(nonatomic,readonly) NSInteger topCapHeight __TVOS_PROHIBITED; // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1
- (void)test2 { UIImage *iconView = [[UIImage alloc] init]; NSInteger leftCapWidth = iconView.size.width * 0.5; NSInteger topCapHeight = iconView.size.height * 0.5; UIImage *newIconView = [iconView stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight]; }