UIImage 图片拉伸之局部拉伸

参阅:iOS聊天气泡的处理——图片拉伸

图片源文件.png

拉伸后的效果.png

extension UIImage{
    /// 拉伸图片(四角不拉伸,只拉伸边长的中心区段)
    ///
    /// - Parameter sscale: 边长中心区段的拉伸比例
    /// - Returns: 拉伸后的图片
    func stretchableImage(centerStretchScale sscale:CGFloat) -> UIImage {
        let leftRight = (self.size.width - self.size.width * sscale)/2.0
        let topBottom = (self.size.height - self.size.height * sscale)/2.0
        return self.resizableImage(withCapInsets: UIEdgeInsets.init(top: topBottom, left: leftRight, bottom: topBottom, right: leftRight), resizingMode: .stretch)
    }
}
/// 调用
let originalImg = UIImage.init(named: "originalImg")
let stretchImg = originalImg.stretchableImage(centerStretchScale: 1.0/6.0)
imageV.image = stretchImg

你可能感兴趣的:(UIImage 图片拉伸之局部拉伸)