How to easily resize/optimize an image size with iOS?

后端 未结 18 1400
温柔的废话
温柔的废话 2020-11-22 11:05

My application is downloading a set of image files from the network, and saving them to the local iPhone disk. Some of those images are pretty big in size (widths larger tha

18条回答
  •  [愿得一人]
    2020-11-22 11:40

    Swift Version

    func resizeImage(image: UIImage, newWidth: CGFloat) -> UIImage? {
    
        let scale = newWidth / image.size.width
        let newHeight = CGFloat(200.0)
        UIGraphicsBeginImageContext(CGSize(width: newWidth, height: newHeight))
        image.draw(in: CGRect(x: 0, y: 0, width: newWidth, height: newHeight))
    
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
    
        return newImage
    }
    

提交回复
热议问题