IOS下图片的拉伸
2013-10-23 10:25
204 查看
效果,类似QQ聊天 背景那个泡泡,或者短信泡泡。
以前有个方法是:
这个方法,使用后发现,并不是那么好用,查了资料说是左边、上边指定好后,相当于这一块不变,其他拉伸(另有资料说是相对的,即不只左边上边的区域,而是只同样大小的四个角不拉伸,中间的拉伸,暂不验证了,后面再去验证)
IOS5以后的一个方法
这个方法比较好理解,后面的参数 就是 设定一个矩形。范围就是 (上,左,下,右)
比如你的图片是120像素的圆角矩形,你想四个角不变,中间随意拉伸。假设每个角都可以用一个10*10的框框框起来。
那你可以使用 (10,10,10,10) ,在你的图片中画一个(距顶10,距左10,距底10,距右10)的矩形,表示该范围内拉伸。
但是,这个图片的使用就需要注意了,图片要使用1X大小的。
例子:
以前有个方法是:
@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)];
相关文章推荐
- iOS杂谈-图片拉伸的实现
- iOS开发笔记--图片拉伸技巧
- ios 图片拉伸的几种方法
- iOS图片自由拉伸
- IOS中图片拉伸,类似Android中的点9图片
- iOS图片拉伸技巧
- iOS仿keep5.3.0版本健身等级界面实现UITableView头部图片下拉纵向拉伸效果
- iOS图片拉伸技巧
- iOS 图片拉伸 resizableImageWithCapInsets
- iOS-使用Xcode拉伸图片
- iOS小技巧 - 图片无损拉伸的三个方法
- iOS-图片的拉伸
- IOS中图片拉伸代码实现
- iOS图片拉伸技巧
- iOS图片拉伸技巧—— resizableImageWithCapInsets
- iOS图片拉伸技巧
- iOS经典讲解之图片拉伸的几种方法
- ios之图片的拉伸、UIImageView的stretchableImageWithLeftCapWidth方法详解
- ios UIImage图片拉伸 resizableImageWithCapInsets:
- 图片拉伸:IOS开发UIImage中stretchableImageWithLeftCapWidth