swift -> 修改图片UIImage宽度, 并 高度自适应

    func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage {
        
        let scale = newWidth / image.size.width
        let newHeight = image.size.height * scale
        UIGraphicsBeginImageContext(CGSize(width:newWidth, height:newHeight))
        image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        
        return newImage!
    }
let img1:UIImage = resizeImage(image: UIImage.init(named: "guide_1")!,newWidth:screenWidth);

猜你喜欢

转载自mft.iteye.com/blog/2368283