iOS图片拉伸的三种方法

方法一:

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

1 // use resizableImageWithCapInsets: and capInsets.
3 - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight __TVOS_PROHIBITED;
4 @property(nonatomic,readonly) NSInteger leftCapWidth __TVOS_PROHIBITED;   // default is 0. if non-zero, horiz. stretchable. right cap is calculated as width - leftCapWidth - 1
5 @property(nonatomic,readonly) NSInteger topCapHeight __TVOS_PROHIBITED;   // default is 0. if non-zero, vert. stretchable. bottom cap is calculated as height - topCapWidth - 1
1  // 拉伸区域距离左端宽度
2  NSInteger leftCapWidth = image.size.width * 0.5f;
3  // 拉伸区域距离顶端高度
4  NSInteger topCapHeight = image.size.height * 0.5f;
5  // 重新给image赋值
6  image = [image stretchableImageWithLeftCapWidth:leftCapWidth topCapHeight:topCapHeight]; 

方法二:

通过设置UIEdgeInsets的left、right、top、bottom来分别指定图片拉伸区域距离左端宽度、右端宽度、顶端高度、底端高度
//通过设置UIEdgeInsets的left、right、top、bottom来分别指定图片拉伸区域距离左端宽度、右端宽度、顶端高度、底端高度
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets NS_AVAILABLE_IOS(5_0); // create a resizable version of this image. the interior is tiled when drawn.
CGFloat top = 10; // 拉伸区域距离顶端高度
CGFloat bottom = 10 ; // 拉伸区域距离底端高度
CGFloat left = 20; // 拉伸区域距离左端宽度
CGFloat right = 20; // 拉伸区域距离右端宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 拉伸重新给image赋值
image = [image resizableImageWithCapInsets:insets];

方法三:

在iOS6.0中,UIImage又提供了一个方法处理图片拉伸,对比iOS5.0中的方法,多了一个UIImageResizingMode参数,用来指定拉伸的模式:
UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
/**在iOS6.0中,UIImage又提供了一个方法处理图片拉伸,对比iOS5.0中的方法,多了一个UIImageResizingMode参数,用来指定拉伸的模式:
*  UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
*  UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片
*/
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode NS_AVAILABLE_IOS(6_0); // the interior is resized according to the resizingMode
CGFloat top = 10; // 拉伸区域距离顶端高度
CGFloat bottom = 10 ; //拉伸区域距离底端高度
CGFloat left = 20; // 拉伸区域距离左端宽度
CGFloat right = 20; // 拉伸区域距离右端宽度
UIEdgeInsets insets = UIEdgeInsetsMake(top, left, bottom, right);
// 拉伸后重新给image赋值
 image = [image resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
时间: 2025-01-02 01:13:48

iOS图片拉伸的三种方法的相关文章

iOS图片拉伸的几种方法

系统至ios6之后,关于图片拉伸的方法已经扩展至3个函数: 1.ios4提供的方法: - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight 这个函数是UIImage的一个实例函数,它的功能是创建一个内容可拉伸,而边角不拉伸的图片,需要两个参数,第一个是不拉伸区域距离左边框的宽度,第二个参数是不拉伸区域距离上边框的宽度,其操作本质是对一个像

iPhone图片拉伸的几种方法

iPhone图片拉伸的几种方法 UIImageResizingModeTile和 UIImageResizingModeStretch两种模式,从名字就可以看出,是平铺模式和拉伸模式.平铺就是复制你Insets指定的矩形区域块来填充你所指定的图片区域,而拉伸就是通过拉伸你Insets指定的矩形区域块来填充你 所需的图片区域. 系统至ios6之后,关于图片拉伸的方法已经扩展至3个函数: 1.ios4提供的方法: - (UIImage *)stretchableImageWithLeftCapWid

iOS拨打电话(三种方法)

iOS拨打电话(三种方法) 查了很多地方的关于iOS程序拨打电话,大都不全,今天我总结了三种方法,各有不同,拿来给大家分享,希望给大家有所帮助 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",@"186xxxx6979"];    //            NSLog(@"s

c# pictureBox1.Image的获得图片路径的三种方法 winform

代码如下:c# pictureBox1.Image的获得图片路径的三种方法 winform 1.绝对路径:this.pictureBox2.Image=Image.FromFile("D:\\001.jpg"); 2.相对路径:Application.StartupPath; 可以得到程序根目录 string picPath=Application.StartupPath+"\\1.gif"; 3.获得网络图片的路径 this.pictureBox2.Image=

iOS UIimage的拉伸的三种方法

第一种方法 // - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight __TVOS_PROHIBITED; //leftCapWidth:左边不拉伸区域 //topCapHeight:上面不拉伸区域 UIImage *image =[UIImageimageNamed:@"chatdetail_info_other"]; UI

用Fiddler可以设置浏览器的UA 和 手动 --Chrome模拟手机浏览器(iOS/Android)的三种方法,亲测无误!

附加以一种软件的方法是:用Fiddler可以设置浏览器的UA 以下3种方法是手动的 通过伪装User-Agent,将浏览器模拟成Android设备. 第一种方法:新建Chrome快捷方式 右击桌面上的Chrome浏览器图标,在弹出的右键菜单中选择“复制”,复制一个图标副本到桌面.右击该副本,选择“属性”,打开相应的对话框,在“目标”文本框的字符后面添加以下语句:“--user-agent="Android"”,如下图: 注意user前面是两个“-”,并且“chrome.exe”与“--

iOS 处理缓存的三种方法

缓存处理是个相当头疼的事情,要根据需要综合应用不同的策略.总的来说有以下几种情况: 1.URL缓存,例如社交应用的帖子浏览,要在viewDidAppear:里面进行URL缓存.简单来说就是用NSURLCache类,首先在AppDelegate.m里面的 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions:方法里面创建一个NSURLC

iOS 创建多线程的三种方法

<span style="font-size:18px;"><strong>(1)//通过NSObject的方法创建线程</strong></span> //(这个方法会自动开辟一个后台线程,参数1:在这个后台线程中执行的方法,参数2:用于传递参数) [self performSelectorInBackground:@selector(banZhuanPlus) withObject:nil]; (2)//通过NSThread创建线程(

iOS延时执行的三种方法

@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 1. NSTimer,可以设置