之前介绍过通过 stretchableImageWithLeftCapWidth:topCapHeight: 方法来实现可伸缩图片;

可看这篇随笔:使用 stretchableImageWithLeftCapWidth 方法实现可伸缩图片

iOS5 中提供了一个新的 UIImage 对象实例方法:resizableImageWithCapInsets:,可以将图片转换为以某一偏移值为偏移的可伸缩图片(偏移值内的图片内容将不被拉伸或压缩)。

stretchableImageWithLeftCapWidth:topCapHeight: 模式(此方法已经过期,推荐用下面的resizableImageWithCapInsets: 方法);

resizableImageWithCapInsets: 模式还能实现类似图片渐变的效果,更灵活控制。

效果如下:

ViewController.h

 #import <UIKit/UIKit.h>

 @interface ViewController : UIViewController
@end

ViewController.m

 #import "ViewController.h"

 @interface ViewController ()
- (void)layoutUI;
@end @implementation ViewController
#define kWidthOfButton 160.0
#define kHeightOfButton 49.0 - (void)viewDidLoad {
[super viewDidLoad]; [self layoutUI];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)layoutUI {
//普通模式
UIImage *img = [UIImage imageNamed:@"blueButton"]; //size: 39*39
UIButton *btnNormal = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 40.0, kWidthOfButton, kHeightOfButton)];
[btnNormal setTitle:@"btnNormal" forState:UIControlStateNormal];
[btnNormal setBackgroundImage:img forState:UIControlStateNormal];
[self.view addSubview:btnNormal]; //stretchableImageWithLeftCapWidth:topCapHeight:模式(此方法已经过期,推荐用下面的resizableImageWithCapInsets:方法)
UIImage *imgStretchable = [img stretchableImageWithLeftCapWidth:16.0 topCapHeight:16.0];
UIButton *btnStretchable = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 50.0+kHeightOfButton, kWidthOfButton, kHeightOfButton)];
[btnStretchable setTitle:@"btnStretchable" forState:UIControlStateNormal];
[btnStretchable setBackgroundImage:imgStretchable forState:UIControlStateNormal];
[self.view addSubview:btnStretchable]; //resizableImageWithCapInsets:模式;相比stretchableImageWithLeftCapWidth:topCapHeight:模式来说,resizableImageWithCapInsets:模式还能实现类似图片渐变的效果,更灵活控制
UIImage *imgResizable = [img resizableImageWithCapInsets:UIEdgeInsetsMake(30.0, 16.0, 9.0, 16.0)];
UIButton *btnResizable = [[UIButton alloc] initWithFrame:CGRectMake(20.0, 60.0+(kHeightOfButton*), kWidthOfButton, kHeightOfButton)];
[btnResizable setTitle:@"btnResizable" forState:UIControlStateNormal];
[btnResizable setBackgroundImage:imgResizable forState:UIControlStateNormal];
[self.view addSubview:btnResizable];
} @end

使用 resizableImageWithCapInsets 方法实现可伸缩图片的更多相关文章

  1. 使用 stretchableImageWithLeftCapWidth 方法实现可伸缩图片

    - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...

  2. php 图片上传的公共方法(按图片宽高缩放或原图)

    写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...

  3. <canvas>drawImage()方法无法显示图片

    在书上看到用<canvas>绘制图像就动手试试,刚开始,我的代码是这样的: <!DOCTYPE html> <html> <head> <meta ...

  4. WPF:通过BitmapSource的CopyPixels和Create方法来切割图片

    原文 WPF:通过BitmapSource的CopyPixels和Create方法来切割图片 BitmapSource是WPF图像的最基本类型,它同时提供两个像素相关的方法就是CopyPixels和C ...

  5. [Android]ListView的Adapter.getView()方法中延迟加载图片的优化

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...

  6. java中getBytes方法可能使图片文件产生的问题

    InputStream is = new FileInputStream(fl); ImageInputStream iis = ImageIO.createImageInputStream(is); ...

  7. createObjectURL方法 实现本地图片预览

    ie6 可以直接显示本本地路径的图片 如: <img src="file://c:/3.jpg" />  ~~~网上都说ie7就不支持这种文件系统路径的url,但测试 ...

  8. 在drawRect:方法中绘制图片,文字以及Core Graphics 框架的了解

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000 } p.p2 { margin: 0.0px 0. ...

  9. 使用readAsDataURL方法预览图片

    使用FileReader接口的readAsDataURL方法实现图片的预览. 在FileReader出现之前,前端的图片预览是这样实现的:把本地图片上传到服务器,服务器把图片地址返回,并把它替换到图片 ...

随机推荐

  1. css优化建议

    1.不要使用过小的图片做背景平铺.这就是为何很多人都不用 1px 的原因,这才知晓.宽高 1px 的图片平铺出一个宽高 200px 的区域,需要 200*200=40, 000 次,占用资源. 2.无 ...

  2. bash里wget失败

    直接使用wget是可以的,然而在shell脚本里却不行,后来发现原来是换行符的问题,编辑器默认的是\r\n,一不留神,自己把自己坑了

  3. Android 后台发送邮件 (收集应用异常信息+Demo代码)

    上一次说了如何收集我们已经发布的应用程序的错误信息,方便我们调试完善程序.上次说的收集方法主要是把收集的信息通过Http的post请求把相关的异常信息变成请求参数发送到服务器.这个对做过web开发的人 ...

  4. 聊聊Google face api

    图像处理开源了很多东西,保存下一些基础的东西,以用来follow最新的东西. Google Face API 是什么? Google 的 Face API 用于面部检测,从图片中找出人的面部,以及位置 ...

  5. 【C】——fread函数和read函数的区别

    1,fread是带缓冲的,read不带缓冲. 2,fopen是标准c里定义的,open是POSIX中定义的. 3,fread可以读一个结构.read在linux/unix中读二进制与普通文件没有区别. ...

  6. SpringBoot2 启动报错 Failed to auto-configure a DataSource

    今天Spring Boot 2.0正式版发布,寻思着搭个小demo尝试一下Spring Boot的新特性,使用idea创建项目.在选择组件时添加了mysql.mybatis 然后在第一次启动的时候启动 ...

  7. 微信小程序——页面之间传递值

    小程序页面传值的方式: 1.正向传值:上一页面 -->  下一页面 url传值 本地储存 全局的app对象 2.反向传值:下一页面 -->  上一页面 本地储存 全局的app对象 先说一下 ...

  8. 解决studio的URI is not registered (Setting|Language&Frameworks|Schemas and DTDs)

    高高兴兴过完国庆来上班,studio一打开发现布局文件跟不进去,点进去就到了R文件里,layout的文件里 xmlns:android="http://schemas.android.com ...

  9. Linux中通过/proc/stat等文件计算Cpu使用率

    Linux平台Cpu使用率的计算 proc文件系统 /proc文件系统是一个伪文件系统,它只存在内存当中,而不占用外存空间.它以文件系统的方式为内核与进程提供通信的接口.用户和应用程序可以通过/pro ...

  10. 情商UP:不遵守八小时工作制,你就能富起来??

    从工作第一天起,我们就默认了八小时工作制,但从没有想过,这是不是最高效的工作时间. 事实上,八小时工作制并没有科学依据,不过是多年来约定俗成的习惯罢了. 那些坚守八小时工作制的人和敢于打破它的人,都获 ...