图片拉伸共有三种方法:
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight
示例:
// 左端盖宽度
NSInteger leftCapWidth = image.size.width * 0.5f;
// 顶端盖高度
NSInteger topCapHeight = image.size.height * 0.5f;
// 重新赋值
image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
- (UIImage *)resizableImageCapInsets:(UIEdgeInsets)Insets
// 重新赋值
CGFloat top = 30; // 顶端盖高度
CGFloat bottom = 30 ; // 底端盖高度
CGFloat left = 35; // 左端盖宽度
CGFloat right = 35; // 右端盖宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 伸缩后重新赋值
image = [image resizableImageWithCapInsets:insets];
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode
// 重新赋值
CGFloat top = 30; // 顶端盖高度
CGFloat bottom = 30 ; // 底端盖高度
CGFloat left = 35; // 左端盖宽度
CGFloat right = 35; // 右端盖宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 伸缩后重新赋值 UIImageResizingModeTile:平铺 UIImageResizingModeStretch:拉伸
image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeTile];
siwft 实现
var image = UIImage(named:"bg.png")
// 左端盖宽度
var leftCapWidth:Int = Int(image!.size.width * 0.5)
// 顶端盖高度
var topCapHeight:Int = Int(image!.size.height * 0.5)
image = image!.stretchableImageWithLeftCapWidth(leftCapWidth,topCapHeight: topCapHeight)