处理图片拉伸的方式有很多,以下参考了http://www.jianshu.com/p/80290e6ae9ac, http://www.jianshu.com/p/1110109f43f5
两篇文章的分享,总结如下,仅为自己使用方便.
1 代码方式
1.1 iOS 5.0之前
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
UIImage *image = [UIImage imageNamed:@"chat"];
// 1 = width - leftCapWidth - right
// 1 = height - topCapWidth - bottom
UIImage *reszingImage = [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
[self.btn setBackgroundImage: reszingImage forState:UIControlStateNormal];
1.2 iOS 5.0
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
// 默认是平铺
UIImage *resizingImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
1.3 iOS 6.0
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
// 1.创建图片对象
UIImage *image = [UIImage imageNamed:@"chat"];
// 2.创建可拉伸的图片(告诉图片什么地方需要拉伸)
CGFloat imageW = image.size.width;
CGFloat imageH = image.size.height;
/*UIImageResizingModeStretch:拉伸模式,通过拉伸UIEdgeInsets指定的矩形区域来填充图片
UIImageResizingModeTile:平铺模式,通过重复显示UIEdgeInsets指定的矩形区域来填充图片*/
UIImage *resizingImage = [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageH * 0.5, imageW * 0.5, imageH * 0.5 - 1, imageW * 0.5 - 1) resizingMode:UIImageResizingModeTile];
网友评论