图片拉伸共有三种方法:
- (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)
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。