一、概述
图片拉伸在移动开发中特别常见,比如常用的即时通讯应用中的聊天气泡就需要根据文字长度对背景图片进行拉伸自适应。在Android中实现图片的拉伸特别特别简单,甚至不用写一行代码,直接使用.9图片进行划线即可。但是iOS就没这么简单了,比如对于下面的一张图片(原始尺寸:200*103):

我们不做任何处理,直接将它用作按钮的背景图片:
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 | // // ViewController.m // ChatBgTest // // Created by 李峰峰 on 2017/1/23. // Copyright © 2017年 李峰峰. All rights reserved. // #import "ViewController.h" @ interface ViewController ( ) @ end @ implementation ViewController - ( void ) viewDidLoad { [ super viewDidLoad ] ; [ self addBtn ] ; } - ( void ) addBtn { // 创建一个按钮 UIButton * btn = [ UIButton buttonWithType : UIButtonTypeCustom ] ; // 设置按钮的frame btn . frame = CGRectMake ( 50 , 300 , 300 , 103 ) ; // 加载图片 UIImage * image = [ UIImage imageNamed : @ "chat_bg" ] ; // 设置按钮的背景图片 [ btn setBackgroundImage : image forState : UIControlStateNormal ] ; // 将按钮添加到控制器的view [ self . view addSubview : btn ] ; } @ end |
运行效果如下:

可以看到图片被明显拉伸,显示效果较差。今天我们研究内容就是图片自适应拉伸。
二、图片自适应拉伸
1、iOS5之前
iOS中有个叫端盖(end cap)的概念,用来指定图片中的哪一部分不用拉伸,如下图:设置topCapHeight、leftCapWidth、bottomCapHeight、lerightCapWidth,图中的黑色区域就是图片拉伸的范围,也就是说边上的不会被拉伸。

使用UIImage的下面这个方法,可以通过设置端盖宽度返回一个经过拉伸处理的UIImage对象:
| - ( UIImage * ) stretchableImageWithLeftCapWidth : ( NSInteger ) leftCapWidth topCapHeight : ( NSInteger ) topCapHeight ; |
这个方法只有2个参数,leftCapWidth代表左端盖宽度,topCapHeight代表上端盖高度。系统会自动计算出右端盖宽度rightCapWidth和底端盖高度bottomCapHeight,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | /** 第一种拉伸方式(iOS5之前) */ - ( void ) stretchTest1 { // 创建一个按钮 UIButton * btn = [ UIButton buttonWithType : UIButtonTypeCustom ] ; // 设置按钮的frame btn . frame = CGRectMake ( 50 , 300 , 300 , 103 ) ; // 加载图片 UIImage * image = [ UIImage imageNamed : @ "chat_bg" ] ; // 设置左边端盖宽度 NSInteger leftCapWidth = image . size . width * 0.5f ; // 设置上边端盖高度 NSInteger topCapHeight = image . size . height * 0.5f ; UIImage * newImage = [ image stretchableImageWithLeftCapWidth : leftCapWidth topCapHeight : topCapHeight ] ; // 设置按钮的背景图片 [ btn setBackgroundImage : newImage forState : UIControlStateNormal ] ; // 将按钮添加到控制器的view [ self . view addSubview : btn ] ; } |
这样一来,其实我们图片的可拉伸范围只有1 * 1,所以再怎么拉伸都不会影响图片的外观,运行效果如下:

现在再看一下效果是不是好多了。
2、iOS5
在iOS 5.0中,UIImage又有一个新方法可以处理图片的拉伸问题:
| - ( UIImage * ) resizableImageWithCapInsets : ( UIEdgeInsets ) capInsets |
| 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类型的参数,可以通过设置UIEdgeInsets中的CGFloat top, left, bottom, right就是用来设置上端盖、左端盖、下端盖、右端盖的尺寸(逆时针方向)。
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 | /** 第二种拉伸方式(iOS5) */ - ( void ) stretchTest2 { // 创建一个按钮 UIButton * btn = [ UIButton buttonWithType : UIButtonTypeCustom ] ; // 设置按钮的frame btn . frame = CGRectMake ( 50 , 300 , 300 , 103 ) ; // 加载图片 UIImage * image = [ UIImage imageNamed : @ "chat_bg" ] ; // 设置端盖的值 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 ] ; // 将按钮添加到控制器的view [ self . view addSubview : btn ] ; } |
运行效果与第一种一样,就不再截图了。
3、iOS6
在iOS6.0中,UIImage又提供了一个方法处理图片拉伸:
| - ( UIImage * ) resizableImageWithCapInsets : ( UIEdgeInsets ) capInsets resizingMode : ( UIImageResizingMode ) resizingMode |
相比iOS5中的方法多了一个resizingMode参数:
| 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 20 21 22 23 24 25 26 27 28 29 30 31 | /** 第三种拉伸方式(iOS6) */ - ( void ) stretchTest3 { // 创建一个按钮 UIButton * btn = [ UIButton buttonWithType : UIButtonTypeCustom ] ; // 设置按钮的frame btn . frame = CGRectMake ( 50 , 300 , 300 , 103 ) ; // 加载图片 UIImage * image = [ UIImage imageNamed : @ "chat_bg" ] ; // 设置端盖的值 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 ] ; // 将按钮添加到控制器的view [ self . view addSubview : btn ] ; } |
运行效果与第一种一样,就不再截图了。