The simplest way to resize an UIImage?

前端 未结 30 3328
迷失自我
迷失自我 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:48

    Rogerio Chaves answer as a swift extension

    func scaledTo(size: CGSize) -> UIImage{
        UIGraphicsBeginImageContextWithOptions(size, false, 0.0);
        self.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        let newImage:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()
        return newImage
    }
    

    And also bonus

    func scaledTo(height: CGFloat) -> UIImage{
        let width = height*self.size.width/self.size.height
        return scaledTo(size: CGSize(width: width, height: height))
    }
    

提交回复
热议问题