Swift - 九宫格图片缩放总结样例

时间:2024-03-31 14:32:56

1,图片左中右三宫格缩放形式

//左右14像素不变形,中间缩放
let imgTrackRight = UIImage(named:"slider_max")
let imgRight = imgTrackRight!.stretchableImageWithLeftCapWidth(14, topCapHeight:0)
slider.setMaximumTrackImage(imgRight, forState:UIControlState.Normal)

2,图片上中下三宫格缩放形式

//上下10像素不变形,中间缩放
let imgBgSouce = UIImage(named:"bg")
let imgBg = imgBgSouce!.stretchableImageWithLeftCapWidth(0, topCapHeight:10)

3,图片9宫格缩放

//左右14像素,上下10像素不变形,中间缩放
let imgBgSouce = UIImage(named:"bg")
let imgBg = imgBgSouce!.stretchableImageWithLeftCapWidth(14, topCapHeight:10)