處理圖片拉伸的方式有很多,以下參考了http://www.lxweimin.com/p/80290e6ae9ac, http://www.lxweimin.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];