iOS图片拉伸的4种方法

时间:2022-12-03 20:25:42

假如下面的一张图片,是用来做按钮的背景图片的,原始尺寸是(128 * 112)

iOS图片拉伸的4种方法

按钮背景图片.png

我们通过代码将这张图片设置为按钮的背景图片,假如我们将创建好的按钮的宽高设置为:(w=200, h=50)代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//
// viewcontroller.m
// ios图片拉伸总结
//
// created by sunshine on 15/6/29.
// copyright (c) 2015年 yotrolz. all rights reserved.
//
 
#import "viewcontroller.h"
 
@interface viewcontroller ()
 
@end
 
@implementation viewcontroller
 
- (void)viewdidload {
 [super viewdidload];
 
 // 创建一个按钮
 uibutton *btn = [uibutton buttonwithtype:uibuttontypecustom];
 
 // 设置按钮的frame
 btn.frame = cgrectmake(100, 300, 200, 50);
 
 // 加载图片
 uiimage *image = [uiimage imagenamed:@"chat_send_nor"];
 
 // 设置按钮的背景图片
 [btn setbackgroundimage:image forstate:uicontrolstatenormal];
 
 // 将按钮添加到控制器的view
 [self.view addsubview:btn];
 
}
 
- (void)didreceivememorywarning {
 [super didreceivememorywarning];
 // dispose of any resources that can be recreated.
}
 
@end

这是你发现运行的结果完全出乎你的意料(搓的无极限),如图:

iOS图片拉伸的4种方法

运行效果图1.png

原因分析:是将原是尺寸为w=128 * h=112的图片拉伸成了w=200, h=50;

解决方案:
1.找美工mm重做一张较大的图片,这样的话就会出现软件包将来会变大,占用空间更大;如果我们要经常修改按钮的frame,你是想让mm杀你的节奏~~,显然不可行;
2.苹果为我们提供了关于图片拉伸的api,我们可以直接利用代码实现,是不是很牛x;

利用苹果提供的api来拉伸图片(目前发现的有四种):

方式一(ios5之前):

如下图:设置topcapheight、leftcapwidth、bottomcapheight、lerightcapwidth,图中的黑色区域就是图片拉伸的范围,也就是说边上的不会被拉伸.
通过下面的方法我们可以设置:

// 官方api说明
// - stretchableimagewithleftcapwidth:topcapheight:(ios 5.0)
// creates and returns a new image object with the specified cap values.

说明:这个方法只有2个参数,leftcapwidth代表左端盖宽度,topcapheight代表上端盖高度。系统会自动计算出右端盖宽度rightcapwidth和底端盖高度bottomcapheight,算法如下:

?
1
2
3
4
5
// 系统会自动计算rightcapwidth
rightcapwidth = image.width - leftcapwidth - 1;
 
// 系统会自动计算bottomcapheight
bottomcapheight = image.height - topcapheight - 1

这样一来,其实我们图片的可拉伸范围只有1 * 1,所以再怎么拉伸都不会影响图片的外观;
具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
// 加载图片
uiimage *image = [uiimage imagenamed:@"chat_send_nor"];
 
// 设置左边端盖宽度
nsinteger leftcapwidth = image.size.width * 0.5;
// 设置上边端盖高度
nsinteger topcapheight = image.size.height * 0.5;
 
uiimage *newimage = [image stretchableimagewithleftcapwidth:leftcapwidth topcapheight:topcapheight];
 
// 设置按钮的背景图片
[btn setbackgroundimage:newimage forstate:uicontrolstatenormal];

运行效果:

iOS图片拉伸的4种方法

运行效果图2.png

方式二:(ios5)

利用下面的方法:

?
1
2
3
4
5
6
7
// 官方api说明
- (uiimage *)resizableimagewithcapinsets:(uiedgeinsets)capinsets ns_available_ios(5_0);
// create a resizable version of this image. the interior is tiled when drawn.
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;

说明:uiedgeinsets中的cgfloat top, left, bottom, right就是用来设置上端盖、左端盖、下端盖、右端盖的尺寸(逆时针方向);

具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// 加载图片
uiimage *image = [uiimage imagenamed:@"chat_send_nor"];
 
// 设置端盖的值
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 *newimage = [image resizableimagewithcapinsets:edgeinsets];
 
// 设置按钮的背景图片
[btn setbackgroundimage:newimage forstate:uicontrolstatenormal];

运行效果:

iOS图片拉伸的4种方法

运行效果图3.png

方式三:(ios6)

利用下面的方法:

 

复制代码 代码如下:
- (uiimage *)resizableimagewithcapinsets:(uiedgeinsets)capinsets resizingmode:(uiimageresizingmode)resizingmode ns_available_ios(6_0);
// the interior is resized according to the resizingmode

 

 

说明:相比ios5中的方法多了一个resizingmode参数

?
1
2
3
4
typedef ns_enum(nsinteger, uiimageresizingmode) {
 uiimageresizingmodetile, // 平铺模式,通过重复显示uiedgeinsets指定的矩形区域来填充图片
 uiimageresizingmodestretch, // 拉伸模式,通过拉伸uiedgeinsets指定的矩形区域来填充图片
};

具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// 加载图片
uiimage *image = [uiimage imagenamed:@"chat_send_nor"];
 
// 设置端盖的值
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);
// 设置拉伸的模式
uiimageresizingmode mode = uiimageresizingmodestretch;
 
// 拉伸图片
uiimage *newimage = [image resizableimagewithcapinsets:edgeinsets resizingmode:mode];
 
// 设置按钮的背景图片
[btn setbackgroundimage:newimage forstate:uicontrolstatenormal];

运行效果:

iOS图片拉伸的4种方法

运行效果图4.png

方式4:(最简单的一种方式)

iOS图片拉伸的4种方法

设置slicing属性.png

iOS图片拉伸的4种方法

设置后.png

是不是so easy~~

运行效果:

iOS图片拉伸的4种方法

运行效果5.png

备注:上面所有通过代码来拉伸图片的方法都是返回一个拉伸后的新图片。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://www.jianshu.com/p/c9cbbdaa9b02