IOS下图片的拉伸

效果,类似QQ聊天 背景那个泡泡,或者短信泡泡。

以前有个方法是:

@interface UIImage(UIImageDeprecated)

// use resizableImageWithCapInsets: and capInsets.

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
@property(nonatomic,readonly) NSInteger leftCapWidth;   // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1
@property(nonatomic,readonly) NSInteger topCapHeight;   // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1

@end

stretchableImageWithLeftCapWidth
这个方法,使用后发现,并不是那么好用,查了资料说是左边、上边指定好后,相当于这一块不变,其他拉伸(另有资料说是相对的,即不只左边上边的区域,而是只同样大小的四个角不拉伸,中间的拉伸,暂不验证了,后面再去验证)


IOS5以后的一个方法

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode

@property(nonatomic,readonly) UIEdgeInsets capInsets               NS_AVAILABLE_IOS(5_0);   // default is UIEdgeInsetsZero for non resizable images
@property(nonatomic,readonly) UIImageResizingMode resizingMode NS_AVAILABLE_IOS(6_0); // default is UIImageResizingModeTile

resizableImageWithCapInsets
这个方法比较好理解,后面的参数 就是 设定一个矩形。范围就是 (上,左,下,右)

比如你的图片是120像素的圆角矩形,你想四个角不变,中间随意拉伸。假设每个角都可以用一个10*10的框框框起来。

那你可以使用 (10,10,10,10) ,在你的图片中画一个(距顶10,距左10,距底10,距右10)的矩形,表示该范围内拉伸。

但是,这个图片的使用就需要注意了,图片要使用1X大小的。

例子:

        //不能直接设定2倍
//        UIImage *image = [UIImage imageNamed:@"image@2x"];
//        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];
        //应使用单倍
        UIImage *image = [UIImage imageNamed:@"image"];
        image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0)];







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值