The simplest way to resize an UIImage?

前端 未结 30 2638
迷失自我
迷失自我 2020-11-21 22:38

In my iPhone app, I take a picture with the camera, then I want to resize it to 290*390 pixels. I was using this method to resize the image :

UIImage *newI         


        
30条回答
  •  长发绾君心
    2020-11-21 22:51

    Use this extension, in case you need to resize width/height only with aspect ratio.

    extension UIImage {
        func resize(to size: CGSize) -> UIImage {
            return UIGraphicsImageRenderer(size: size).image { _ in
                draw(in: CGRect(origin: .zero, size: size))
            }
        }
        func resize(width: CGFloat) -> UIImage {
            return resize(to: CGSize(width: width, height: width / (size.width / size.height)))
        }
        func resize(height: CGFloat) -> UIImage {
            return resize(to: CGSize(width: height * (size.width / size.height), height: height))
        }
    }
    

提交回复
热议问题