将小图的尺寸为:24x60 制作成尺寸是150x50的按钮
先按如下的方法:



1. CGSize viewSize = self.view.bounds.size; 
2. UIButton *button = [[UIButton alloc] init];  
3. button.bounds = CGRectMake(0, 0, 150, 50);  
4. button.center = CGPointMake(viewSize.width * 0.5f, viewSize.height * 0.5f);  
5. UIImage *image = [UIImage imageNamed:@"button"];  
6. [button setBackgroundImage:image forState:UIControlStateNormal];  
7. [self.view addSubview:button];


运行效果非常地差。原因很简单,因为原图大小为24x60,现在整张图片被全方位拉伸为150x50,比较严重的是图片的4个角。

不建议采取直接使用大图的方式。原因很简单:1.图片大,导致安装包也大,加载到内存中也大;2.有更好的解决方案。

细看一下图片会变得难看,完全是因为4个角被拉伸了,中间的拉伸并没有明显地丑化外观。因此要想小图片被拉伸后不会变得难看,在图片拉伸的时候,我们只需拉伸图片的中间一块矩形区域即可,不要拉伸边缘部分。

iOS中提供很好用的API帮我们实现上述功能。到iOS 8.4为止,我所知iOS提供了3种图片拉伸的解决方案,接下来分别详细介绍这些方案。

- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- (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

iOS中有个叫端盖(end cap)的概念,用来指定图片中的哪一部分不用拉伸。比如下图中,黑色代表需要被拉伸的矩形区域,上下左右不需要被拉伸的边缘就称为端盖。

image不拉伸 ios ios照片拉伸工具_图片拉伸

使用UIImage的这个方法,可以通过设置端盖宽度返回一个经过拉伸处理的UIImage对象



    1. - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;


    这个方法只有2个参数,leftCapWidth代表左端盖宽度,topCapHeight代表顶端盖高度。系统会自动计算出右端盖宽度(rightCapWidth)和底端盖高度(bottomCapHeight)


    1. // 左端盖宽度
    2. NSInteger leftCapWidth = image.size.width * 0.5f;  
    3. // 顶端盖高度
    4. NSInteger topCapHeight = image.size.height * 0.5f;  
    5. // 重新赋值
    6. image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight];

    调用这个方法后,原来的image并不会发生改变,会产生一个新的经过拉伸的UIImage,所以第6行中需要将返回值赋值回给image变量

    可以发现,图片非常美观地显示出来了



    在iOS 5.0中,UIImage又有一个新方法可以处理图片的拉伸问题






      1. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets


      这个方法只接收一个UIEdgeInsets类型的参数,可以通过设置UIEdgeInsets的left、right、top、bottom来分别指定左端盖宽度、右端盖宽度、顶端盖高度、底端盖高度


      1. CGFloat top = 25; // 顶端盖高度
      2. CGFloat bottom = 25 ; // 底端盖高度
      3. CGFloat left = 10; // 左端盖宽度
      4. CGFloat right = 10; // 右端盖宽度
      5. UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);  
      6. // 伸缩后重新赋值
      7. image = [image resizableImageWithCapInsets:insets];


      iOS 6.0

      在iOS6.0中,UIImage又提供了一个方法处理图片拉伸



        1. - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode


        对比iOS5.0中的方法,只多了一个UIImageResizingMode参数,用来指定拉伸的模式:

        • UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
        • UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片


        1. CGFloat top = 25; // 顶端盖高度
        2. CGFloat bottom = 25 ; // 底端盖高度
        3. CGFloat left = 10; // 左端盖宽度
        4. CGFloat right = 10; // 右端盖宽度
        5. UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);  
        6. // 指定为拉伸模式,伸缩后重新赋值
        7. image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];