使用 resizableImageWithCapInsets 方法实现可伸缩图片
之前介绍过通过 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 方法实现可伸缩图片的更多相关文章
- 使用 stretchableImageWithLeftCapWidth 方法实现可伸缩图片
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap ...
- php 图片上传的公共方法(按图片宽高缩放或原图)
写的用于图片上传的公共方法类调用方法: $upload_name='pic';$type = 'logo_val';$file_name = 'logo_' . $user_id .create_st ...
- <canvas>drawImage()方法无法显示图片
在书上看到用<canvas>绘制图像就动手试试,刚开始,我的代码是这样的: <!DOCTYPE html> <html> <head> <meta ...
- WPF:通过BitmapSource的CopyPixels和Create方法来切割图片
原文 WPF:通过BitmapSource的CopyPixels和Create方法来切割图片 BitmapSource是WPF图像的最基本类型,它同时提供两个像素相关的方法就是CopyPixels和C ...
- [Android]ListView的Adapter.getView()方法中延迟加载图片的优化
以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4139998.html 举个例子吧,以好友列表为例 ListVi ...
- java中getBytes方法可能使图片文件产生的问题
InputStream is = new FileInputStream(fl); ImageInputStream iis = ImageIO.createImageInputStream(is); ...
- createObjectURL方法 实现本地图片预览
ie6 可以直接显示本本地路径的图片 如: <img src="file://c:/3.jpg" /> ~~~网上都说ie7就不支持这种文件系统路径的url,但测试 ...
- 在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. ...
- 使用readAsDataURL方法预览图片
使用FileReader接口的readAsDataURL方法实现图片的预览. 在FileReader出现之前,前端的图片预览是这样实现的:把本地图片上传到服务器,服务器把图片地址返回,并把它替换到图片 ...
随机推荐
- Eigen教程(10)
整理下Eigen库的教程,参考:http://eigen.tuxfamily.org/dox/index.html 混淆 在Eigen中,当变量同时出现在左值和右值,赋值操作可能会带来混淆问题.这一篇 ...
- ubuntu 安装redis
1. 下载安装: cd /tmp wget http://redis.googlecode.com/files/redis-2.2.4.tar.gz tar -zxf redis-2.2.4.tar. ...
- 【Unity】使用Resources类管理资源
最近参考了各位大神的资源,初步学习了Unity的资源管理模式,包括在编辑器管理(使用AssetDatabase)和在运行时管理(使用Resources和AssetBundle).在此简单总结运行时用R ...
- shell文本处理
1.文件整理employee文件中记录了工号和姓名employee.txt:100 Jason Smith 200 John Doe 300 Sanjay Gupta 400 Ashok Sharma ...
- VBA中数据库导出数据到Excel注意事项
Sub ReadDBData() On Error GoTo ErrorHand Dim dbHelper As New dbHelper Dim sqlSQL As String Dim rs As ...
- IIS 7上部署PHP【后续一】
在之前成功在windows 2008+iis7环境下部署php的基础上,今天把之前的挂Q网站转移到了这个服务器下. 文件拷贝到服务器后,问题继续出现. 首先出现的问题是,Mysql的数据库名称和账户密 ...
- solr报错 ERROR SolrDispatchFilter null:ClientAbortException: java.net.SocketException: Broken pipe 原因是nginx截断了请求
[root@localhost nginx]# lltotal 36drwx------. 2 www root 4096 Aug 13 13:25 client_body_tempdrwxr-xr- ...
- PCL点云曲面重建(1)
在测量较小的数据时会产生一些误差,这些误差所造成的不规则数据如果直接拿来曲面重建的话,会使得重建的曲面不光滑或者有漏洞,可以采用对数据重采样来解决这样问题,通过对周围的数据点进行高阶多项式插值来重建表 ...
- jquery 发get post请求
https://www.cnblogs.com/summers/p/3225375.html POST 方法不会缓存数据 $.get(URL,callback); 2个参数 callback 参数是请 ...
- struts2 拦截器配置
CheckLoginInterceptor.java---拦截器具体实现类: package com.sunhoo.hcpms.struts2.action.interceptors; import ...