bg_discountcolor_active@3x.png
如圖我們想把這張圖片拉大, 但保持圓角不變, 肯定不能直接把image放大, swift提供兩個方法
let imageV = UIImageView(frame: CGRect(x: 50, y: 200, width: width, height: height))
var image = UIImage(named: "bg_discountcolor_active")
- image = image?.stretchableImage(withLeftCapWidth: 8, topCapHeight: 8)
這個方法保證圖片從上面8, 左邊8的像素點(diǎn)拉伸 - image = image?.resizableImage(withCapInsets: UIEdgeInsets(top: 8, left: 8, bottom: 8, right: 8), resizingMode: .stretch)
這個方法圈出一個四面間距都為8的區(qū)域, 拉伸的時候只有這塊區(qū)域變大, 只有設(shè)置了值的那個方向的區(qū)域才會被保護(hù)起來
但是在實際使用的時候遇到的問題總是拉伸變形, 甚至?xí)怀鰜硪粔K, 查找原因
圖片尺寸11454(@3x)
那么實際尺寸應(yīng)該是3818, 所以拉伸的間距縱向不能超過9, 橫向不能超過19, 考慮到保存圓角, 這里縱向值應(yīng)該為9, 否則超過一半, 因為iOS的機(jī)制, 超出部分會向外擴(kuò)張, 造成變形
比如: image = image?.stretchableImage(withLeftCapWidth: 20, topCapHeight: 20)
變形圖.png