iOS开发中我们会遇到渐变的背景,内容可变的流式标签,聊天气泡(QQ聊天气泡),由于内容是可变的,宽度和高度同样可变,这样就是导致每次出现的尺寸与之前不一样。如果是需要设置的比较的多,估计美工会烦死,同样也会额外增加的内存开销,所以这个知道一点图片拉伸的技巧会师我们的能使我们APP更加高效,代码更加简洁,事半功倍~从设置的角度来有四种方法可以实现~

Assets设置

首先我们有一个不规则的气泡按钮,大小是35*30,Slices选中水平和垂直方向的距离,宽高设置为1

设置完成之,我们设置Button的背景图片看下加载效果:

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(80, 100, 100, 40)];
UIImage *buttonImage = [UIImage imageNamed:@"Question"];
[button setTitle:@"默认" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[self.view addSubview:button];

模拟器加载效果:

第一个图片是我们需要加载效果,如果我们将Slices设置为None,我们看到的效果是这样的:

看到这里是不是感觉有点兴趣了,为什么设置了上面的选项就可以出现平铺的效果,稍微思考一下,我们在下面的一种方式中将找到答案~

iOS5之前的设置

上图的第二个按钮是我们通过代码设置,不是在Assets中设置,我们先来看一下实现代码:

    //35*30
UIButton *nextButton = [[UIButton alloc] initWithFrame:CGRectMake(80, 180, 100, 40)];
[nextButton setTitle:@"iOS5之前" forState:UIControlStateNormal];
nextButton.layer.borderColor=[[UIColor redColor] CGColor];
nextButton.layer.borderWidth=1.0f;
UIImage *image = [UIImage imageNamed:@"Question"];
// 设置左边端盖宽度 rightCap=width - leftCapWidth - 1
NSInteger leftCapWidth = image.size.width * 0.5;
// 设置上边端盖高度 bottom=height - topCapWidth - 1
NSInteger topCapHeight = image.size.height * 0.5;
UIImage *newImage = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];
[nextButton setBackgroundImage:newImage forState:UIControlStateNormal];
[self.view addSubview:nextButton];

新建图片,设置按钮的背景图片,与一般设置没有什么区别,不过多了stretchableImageWithLeftCapWidth:方法,我们看一下API说明:

@interface UIImage(UIImageDeprecated)

// use resizableImageWithCapInsets: and capInsets.

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

我们需要设置拉伸区域具体图片的上方和左边的距离:

strechWidth=width-leftCapWidth-(width-leftCapWidth-1)=1

strechHeight=height-rightCaopWidth-(height - topCapWidth - 1)=1

所以我们拉伸的区域是1*1,关于上面的leftCapWidth有人可能心里有疑问,为什么leftCapWidth = image.size.width * 0.5,为什么是0.5?自己最开始随便设置一个距离结果图片丑的要死,比如说我们左边设置0.2,上边设置为0.8,我们看到的效果是这样的:

这是极端情况,默认的情况下不管图片是如何的规则,中间的1*1区域是正方形,如果按照上面设置,我们会发现,上下左右四个点组成的图片是极其不规则,因此建议设置在中间,不过有的是有偏离一些也没有太多问题,比如说0.4,0.6对于比例设置大家如果有兴趣的可以之后下载代码,自行研究~

iOS5设置

stretchableImageWithLeftCapWidth在iOS5之后已经废弃,我们可以通过resizableImageWithCapInsets来设置上下左右边距~

resizableImageWithCapInsets与之前的方法一样,同样是通过重置产生新的图片:

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

UIEdgeInsets基本上UI中比较常见:

typedef struct UIEdgeInsets {
CGFloat top, left, bottom, right; // specify amount to inset (positive) for each of the edges. values can be negative to 'outset'
} UIEdgeInsets;

图片效果参考模拟器的第三个按钮,设置代码如下:

     UIImage *image = [UIImage imageNamed:@"Question"];
UIButton *resizableButton=[[UIButton alloc]initWithFrame:CGRectMake(80, 250, 100, 40)];
[resizableButton setTitle:@"iOS5" forState:UIControlStateNormal];
// 设置端盖的值
CGFloat top = image.size.height * 0.5;
CGFloat left = image.size.width * 0.5;
CGFloat bottom = image.size.height * 0.5;
CGFloat right = image.size.width * 0.5; UIEdgeInsets edgeInsets = UIEdgeInsetsMake(top, left, bottom, right);
//拉伸图片
UIImage *edgeImage = [image resizableImageWithCapInsets:edgeInsets];
//背景图片
[resizableButton setBackgroundImage:edgeImage forState:UIControlStateNormal];
[self.view addSubview:resizableButton];

iOS6设置

iOS6的设置方法和iOS5差不多,多了一个参数UIImageResizingMode:

- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode

UIImageResizingMode说明:

typedef NS_ENUM(NSInteger, UIImageResizingMode) {
UIImageResizingModeTile,平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
UIImageResizingModeStretch, };拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片

实际效果参考上图中的最后一个按钮:

    UIImage *image = [UIImage imageNamed:@"Question"];
UIButton *resizableButtonMode=[[UIButton alloc]initWithFrame:CGRectMake(80, 320, 180, 40)];
[resizableButtonMode setTitle:@"iOS6" forState:UIControlStateNormal];
[resizableButtonMode addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
// 设置上左下右边距
CGFloat topMode= image.size.height * 0.5;
CGFloat leftMode= image.size.width * 0.5;
CGFloat bottomMode= image.size.height * 0.5;
CGFloat rightMode= image.size.width * 0.5; UIEdgeInsets edgeInsetsMode= UIEdgeInsetsMake(topMode, leftMode, bottomMode, rightMode); // 拉伸图片
UIImage *edgeModeImage = [image resizableImageWithCapInsets:edgeInsetsMode resizingMode:UIImageResizingModeStretch];
// UIImage *edgeModeImage = [image resizableImageWithCapInsets:edgeInsetsMode resizingMode:UIImageResizingModeTile]; //设置图片
[resizableButtonMode setBackgroundImage:edgeModeImage forState:UIControlStateNormal];
[self.view addSubview:resizableButtonMode];

 项目GitHub地址:https://github.com/SmallElephant/iOS-FEImageStrech

iOS-图片拉伸技巧的更多相关文章

  1. iOS图片拉伸技巧

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...

  2. (转)iOS图片拉伸技巧

    ( 原文博客地址:  http://blog.csdn.net/q199109106q/article/details/8615661) 纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要 ...

  3. iOS图片拉伸技巧-李明杰分享

    http://bbs.itcast.cn/thread-21436-1-1.html 本文目录 "一.iOS5.0之前------------------------------------ ...

  4. iOS图片拉伸技巧—— resizableImageWithCapInsets

    http://blog.csdn.net/chaoyuan899/article/details/19811889

  5. IOS中图片拉伸技巧与方法总结(转载)

    以下内容转载自:http://my.oschina.net/u/2340880/blog/403996 IOS中图片拉伸技巧与方法总结 一.了解几个图像拉伸的函数和方法 1.直接拉伸法 简单暴力,却是 ...

  6. 李洪强iOS开发之图片拉伸技巧

    纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...

  7. iOS 图片拉伸的解释

    以前对于ios的图片拉伸参数一直不太理解,终于看到一篇好文章,转载一下,原文地址:http://blog.csdn.net/q199109106q/article/details/8615661 主要 ...

  8. 博主教你制作类似9patch效果的iOS图片拉伸

    下面张图片,本来是设计来做按钮背景的:   button.png,尺寸为:24x60 现在我们把它用作为按钮背景,按钮尺寸是150x50: // 得到view的尺寸 CGSize viewSize = ...

  9. iOS图片拉伸

    常用的图片拉伸场景有:聊天页面的气泡,需要根据内容拉伸,但圆角拉伸后会变形,为避免圆角拉伸,可以指定拉伸区域.UIImage实体调用以下方法即可指定拉伸区域. - (UIImage *)stretch ...

  10. iOS 图片拉伸 resizableImageWithCapInsets

    UIImage *image =  [[UIImage imageNamed:@"test.png"] resizableImageWithCapInsets:UIEdgeInse ...

随机推荐

  1. 打开div层

    $('#moneyWin').dialog('open').dialog('center').dialog('setTitle', '用户充值');

  2. java根据逗号分隔字符串,后加上单引号

    public class SpiltString { public String spilt(String str) {  StringBuffer sb = new StringBuffer();  ...

  3. UzysAssetsPickerController中文化

    self.labelSelectedMedia.text = NSLocalizedStringFromTable(@"Choose a media", @"UzysAs ...

  4. HttpServletResponse

    Web服务器收到客户端的http请求,会针对每一次请求,分别创建一个用于代表请求的request对象.和代表响应的response对象.request和response对象即然代表请求和响应,那我们要 ...

  5. PHP安装rrdtool扩展

    首先,我的环境是Centos 6.5 PHP版本是5.51.首先安装rrdtool需要的依赖库文件   yum -y install pango* 2.先服务器安装rrdtool  wget http ...

  6. Java.web-application-development-environments-for-macosx

    Java Web Application开发 1 下载需要的软件 使用的软件并没有采用最新的版本,只是采用了次新版本. 1.1 下载Eclipse的jee版本 eclipse-jee-luna-SR2 ...

  7. github的一些指令

  8. vim/vi 复制,删除,粘贴,查找,替换

    1.删除行 光标所在单行,dd  光标所在行以下的5行,5dd 全部删除:dG 2.复制 复制光标所在单行:yy 复制光标以下4行:4yy 3.粘贴 粘贴vi里复制的内容:p 粘贴外部复制过来的内容: ...

  9. APNS推送通知消息负载内容和本地格式字符串

    来源:http://hi.baidu.com/tangly888/blog/item/62948520121870559358074f.html 翻译苹果文档 地址:  翻译:tangly http: ...

  10. 在Openfire中使用自己的数据表之修改配置文件

    目前我使用的Openfire版本是3.10.3,以下使用说明也是在这个版本上做的修改. Openfire提供了两种方式使用用户数据表.一种是安装完成之后默认实现的org.jivesoftware.op ...