原理是拉伸里面的內容,將邊保護起來
方法1:
- 直接傳入保護的范圍损谦,沒有設置圖拉伸的模式蓖扑,默認為UIImageResizingModeTile(瓦片式)就是將圖片以原來的大小就行平鋪顯示
[image resizableImageWithCapInsets:UIEdgeInsetsMake(30, 30, 30, 30)];
- 設置圖片拉伸的模式
[image resizableImageWithCapInsets:UIEdgeInsetsMake(30, 30, 30, 30) resizingMode:UIImageResizingModeStretch];
typedef NS_ENUM(NSInteger, UIImageResizingMode) {
UIImageResizingModeTile,(瓦片)
UIImageResizingModeStretch,(伸展)
};
上述的方法是將圖片沒有保護的部分進行拉伸。
方法2:
[image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
蘋果原始就存在的方法,已經(jīng)被舍棄饲化,這個方法只會拉伸中間 1*1 的區(qū)域
一般傳入的值為圖片大小的一半
計算公式為:
// left(傳入)
// top(傳入)
// width
// height
// right = width - left - 1;(計算)
// 1 = width - left - right;
// bottom = height - top - 1;(計算)
// 1 = height - top - bottom;
方法3:
不需要寫代碼
將圖片拖拽到images.xcassets,具體設置步驟如下圖:
操作步驟圖
將圖片按照上圖方法設置后艰管,圖片會一直可以拉伸