因为要兼容5.0所以还是用了 stretchableImageWithLeftCapWidth: topCapHeight:的方法
//图片拉伸
UIImageView *imageview1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 30, 200.0, 28.0)];
imageview1.image = [UIImage imageNamed:@"1@2x"];
[self.view addSubview:imageview1];
UIImageView *imageview2 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 65.0, 300.0, 28.0)];
imageview2.image = [[UIImage imageNamed:@"1"] stretchableImageWithLeftCapWidth:60.0 topCapHeight:10.0];
// imageview.image = [[UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"1@2x" ofType:@"png"]]] stretchableImageWithLeftCapWidth:5.0 topCapHeight:0.0];
[self.view addSubview:imageview2];
UIImageView *imageview3 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 100.0, 300.0, 28.0)];
imageview3.image = [[UIImage imageNamed:@"1"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:10.0];
[self.view addSubview:imageview3];
UIImageView *imageview4 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 135.0, 300.0, 56.0)];
imageview4.image = [[UIImage imageNamed:@"1"] stretchableImageWithLeftCapWidth:5.0 topCapHeight:5.0];
[self.view addSubview:imageview4];
使用的是@2x的图片,在使用stretchableImageWithLeftCapWidth: topCapHeight:方法时不能加载@2x的图片,不然拉伸效果不对。所以使用imageWithData加载@2x图片的拉伸也是不对。
上面测试了三种拉伸效果,从图片的拉伸效果看其实拉伸的时候从我们设置的LeftCapWidth:开始拉伸。我看人家介绍拉伸的像素点是LeftCapWidth:和topCapHeight:的一个像素点的。但是测试看的如果是长度拉伸的话应该LeftCapWidth:开始的一条像素的拉伸。