ios 按钮图片拉伸_iOS图片拉伸1

iOS目前拉伸不支持四周进行拉伸,中间部分保持不变的方法!!!

iOS支持的是某个部分的拉伸。

实际使用

情形1:按钮的背景图拉伸以适应文字大小

情形2:聊天对话框

按钮.png

对话框.png

一、iOS 5.0之前

iOS中有个叫端盖(end cap)的概念,用来指定图片中的哪一部分不用拉伸。比如下图中,黑色代表需要被拉伸的矩形区域,上下左右不需要被拉伸的边缘就称为端盖。

使用UIImage的这个方法,可以通过设置端盖宽度返回一个经过拉伸处理的UIImage对象

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;

原图.png

参数:

1、leftCapWidth代表左端盖宽度 ;topCapHeight代表顶端盖高度。

2、系统会自动计算出右端盖宽度(rightCapWidth)和底端盖高度(bottomCapHeight),算法如下:

// width为图片宽度

rightCapWidth = width - leftCapWidth - 1;

// height为图片高度

bottomCapHeight = height - topCapHeight - 1

经过计算,你会发现中间的可拉伸区域只有1x1

// stretchWidth为中间可拉伸区域的宽度

stretchWidth = width - leftCapWidth - rightCapWidth = 1;

// stretchHeight为中间可拉伸区域的高度

stretchHeight = height - topCapHeight - bottomCapHeight = 1;

因此,使用这个方法只会拉伸图片中间1x1的区域,并不会影响到边缘和角落。

注意:

1.这个方法在iOS 5.0出来后就过期了

2.这个方法只能拉伸1x1的区域

3.调用这个方法后,原来的image并不会发生改变,会产生一个新的经过拉伸的UIImage

二、iOS 5.0

在iOS 5.0中,UIImage又有一个新方法可以处理图片的拉伸问题

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets

这个方法只接收一个UIEdgeInsets类型的参数,可以通过设置UIEdgeInsets的left、right、top、bottom来分别指定左端盖宽度、右端盖宽度、顶端盖高度、底端盖高度

CGFloat top = 25; // 顶端盖高度

CGFloat bottom = 25 ; // 底端盖高度

CGFloat left = 10; // 左端盖宽度

CGFloat right = 10; // 右端盖宽度

UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);

// 伸缩后重新赋值

image = [image resizableImageWithCapInsets:insets];

注意:

1.这个方法拉伸区域是除了上 、下、 左 、右的部分

三、iOS 6.0

在iOS6.0中,UIImage又提供了一个方法处理图片拉伸

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode

对比iOS5.0中的方法,只多了一个UIImageResizingMode参数,用来指定拉伸的模式:

UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片

UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片

CGFloat top = 25; // 顶端盖高度

CGFloat bottom = 25 ; // 底端盖高度

CGFloat left = 10; // 左端盖宽度

CGFloat right = 10; // 右端盖宽度

UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);

// 指定为拉伸模式,伸缩后重新赋值

image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];

拉伸之后的图片.png

四、对于xib 和 storyboard上面图片的拉伸

5DD505D8-ACC9-4808-8BAC-0737C148A363.png

我们可以给UIButton 和 UIImageView直接进行图片赋值。

1、button可以赋两张图片,一张是backgroundImage和image

1.1 image因为是系统提供的,默认出现在button上文字的左边,并且image会保持原图大小不会缩放。如果有具体的要求不符合,我一般使用自定义button。

1.2 backgroundImage的图片会自动进行button图片的缩放,并且这个缩放是有几种情况的,和UIImageView的一样

2、UIImageView的图片自适应

.contentMode属性就是用来设置这个的。这些具体的解释有很多,

typedef NS_ENUM(NSInteger, UIViewContentMode) {

UIViewContentModeScaleToFill,

UIViewContentModeScaleAspectFit,

UIViewContentModeScaleAspectFill,

UIViewContentModeRedraw,

UIViewContentModeCenter,

UIViewContentModeTop,

UIViewContentModeBottom,

UIViewContentModeLeft,

UIViewContentModeRight,

UIViewContentModeTopLeft,

UIViewContentModeTopRight,

UIViewContentModeBottomLeft,

UIViewContentModeBottomRight,

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值