Swift -> Modify the width of the image UIImage, and adapt the height

    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);

 

 

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326264157&siteId=291194637