之前介绍过通过 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. 获得discuz7.2 目录下所有文件,并写到txt

    # -*- coding: utf-8 -*- import os fileList = [] dzList = [] def GetFileList(path): List = [] for roo ...

  2. 面试问卷-java&Oracle

    东方国信 - 软件开发人员面试问卷(ver1.001.002) 1.    通用编程知识问卷(所有编程人员必做)... 1 1.1      SQL问卷... 1 1.2      翻译... 2 2 ...

  3. Functions: C++'s Programming Modules

    在这一章中要学习以下内容: 函数基础 函数原型 通过value向函数传递参数 设计处理数组的函数 使用const指针参数 设计函数处理文本字符串 设计函数处理结构体 设计函数处理string类型的对象 ...

  4. Java编程的逻辑 (66) - 理解synchronized

    ​本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http: ...

  5. 【进阶修炼】——改善C#程序质量(2)

    16, 元素可变的情况下应避免用数组. 数组是定长的集合,可以考虑用ArrayList或List<T>集合.ArrayList元素是object类型,有装箱的开销,性能较低.另外Array ...

  6. JavaScript:数据类型

    JavaScript中数据类型分为两种: 1.基本数据类型 string number boolean null undefined 2.引用类型 数组.Object.function 一.基本数据类 ...

  7. 《编程之美》practice

    1.2.中国象棋将帅问题 要求:只用一个字节存储变量,输出将帅不照面的所有可能位置. 思路简单,就是穷举让将和帅不在同一列即可,用char高四字节和低四字节分别存储将和帅的位置,位置编号从1到9.代码 ...

  8. [kafka] 003_kafka_主要配置

    1.broker配置 2.Producer主要配置 3.Consumer主要配置 出错提示信息: replication factor: 1 larger than available brokers ...

  9. http://www.gasi.ch/blog/inside-deep-zoom-2/

    Inside Deep Zoom – Part II: Mathematical Analysis Welcome to part two of Inside Deep Zoom. In part o ...

  10. 从Container内存监控限制到CPU使用率限制方案

    转自:http://blog.csdn.net/Androidlushangderen/article/details/50282593 前言 最近在运维我们部门的hadoop集群时,发现了很多Job ...