您的位置:首页 > 移动开发 > IOS开发

IOS下图片的拉伸

2013-10-23 10:25 204 查看
效果,类似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)];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: