The simplest way to resize an UIImage?

前端 未结 30 2604
迷失自我
迷失自我 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 23:07

    Effective approach without stretching image Swift 4

    // Method to resize image
    func resize(image: UIImage, toScaleSize:CGSize) -> UIImage {
                    UIGraphicsBeginImageContextWithOptions(toScaleSize, true, image.scale)
                            image.draw(in: CGRect(x: 0, y: 0, width: toScaleSize.width, height: toScaleSize.height))
                            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
                            UIGraphicsEndImageContext()
                            return scaledImage!
                    }
    

    // Call method

        let resizedImage = self.resize(image: UIImage(named: "YourImageName")!, toScaleSize: CGSize(width: 290, height: 390))
    

提交回复
热议问题