user9472878
user9472878

Reputation:

UIImage resizing scale

I have an image like this:

Have a look at this image

and I'd like to resize this image to a specific width: let's say 200px where the height should also get calculated. (So the image should keep its width-to-height scale)

This was all I get so far:

extension UIImage {
    func resize(newSize1: CGSize) -> UIImage {
        let size = self.size
        let widthRatio  = newSize1.width  / size.width
        let heightRatio = newSize1.height / size.height

        var newSize2: CGSize
        if(widthRatio > heightRatio) {
            newSize2 = CGSize(width: size.width * heightRatio, height: size.height * heightRatio)
        } else {
            newSize2 = CGSize(width: size.width * widthRatio,  height: size.height * widthRatio)
        }
        let rect = CGRect(x: 0, y: 0, width: newSize2.width, height: newSize2.height)
        UIGraphicsBeginImageContextWithOptions(newSize2, false, 1.0)
        self.draw(in: rect)
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return newImage2!
    }
}

But the function takes a CGSize size parameter (width AND height) but I just want to get a function that takes the width parameter.

How to edit my code to solve my problem?
Any help would be very appreciated :)

Note: The UIImage should get a smaller resolution (less pixels), not the Image View itself!

Upvotes: 1

Views: 11715

Answers (3)

carlson
carlson

Reputation: 105

Based on the solution from @deoKasuhal I developed the following approach to scale an image so that the bigger size (width or height) don't exceeds a defined maximum size, defined by the parameter 'base'. Example: if the original size is width = 4000 and height = 3000 and the parameter 'base' is set to 200 the returned image has the size 200 x 150 (or vice versa if height is bigger than width):

extension UIImage {
func resize(toBase base: CGFloat) -> UIImage {
    guard base > 0 else {
        return self
    }
    let widthRatio  = base / max(self.size.width, 1)
    let heightRatio = base / max(self.size.height, 1)
    var updateSize = CGSize()
    
    if (widthRatio > heightRatio) {
        updateSize = CGSize(width:self.size.width * heightRatio, height:self.size.height * heightRatio)
    } else {
        updateSize = CGSize(width:self.size.width * widthRatio,  height:self.size.height * widthRatio)
    }
    UIGraphicsBeginImageContextWithOptions(updateSize, false, UIScreen.main.scale)
    self.draw(in: CGRect(origin: .zero, size: updateSize))
    if let newImage = UIGraphicsGetImageFromCurrentImageContext() {
        UIGraphicsEndImageContext()
        return newImage
    } else {
        UIGraphicsEndImageContext()
        return self
    }
}

}

Upvotes: 0

Ajay saini
Ajay saini

Reputation: 2470

For Swift 4 And Swift 3 use below UIImage extension for resizing image. It's calculated height according to given width.

extension UIImage {
    func resized(toWidth width: CGFloat) -> UIImage? {
        let canvasSize = CGSize(width: width, height: CGFloat(ceil(width/size.width * size.height)))
        UIGraphicsBeginImageContextWithOptions(canvasSize, false, scale)
        defer { UIGraphicsEndImageContext() }
        draw(in: CGRect(origin: .zero, size: canvasSize))
        return UIGraphicsGetImageFromCurrentImageContext()
    }
}

Upvotes: 7

deoKasuhal
deoKasuhal

Reputation: 2897

You can calculate the desired height based on your width and aspect ratio of the image. The same can be applied to the height parameters. Here is a sample of the extension of the UIImage.

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

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

func resize(size: CGSize) -> UIImage {
    let widthRatio  = size.width/self.size.width
    let heightRatio = size.height/self.size.height
    var updateSize = size
    if(widthRatio > heightRatio) {
        updateSize = CGSize(width:self.size.width*heightRatio, height:self.size.height*heightRatio)
    } else if heightRatio > widthRatio {
        updateSize = CGSize(width:self.size.width*widthRatio,  height:self.size.height*widthRatio)
    }
    UIGraphicsBeginImageContextWithOptions(updateSize, false, UIScreen.main.scale)
    self.draw(in: CGRect(origin: .zero, size: updateSize))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage!
}

}

Upvotes: 6

Related Questions