2

I am trying to resize images using the following popular code and it is resizing the image but it is resizing the image as Scale to Fill, I would like to resize them as Aspect Fit. How do I do that?

func resizeImage(image: UIImage, newSize: CGSize) -> (UIImage) {

    let newRect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height).integral
    UIGraphicsBeginImageContextWithOptions(newSize, true, 0)
    let context = UIGraphicsGetCurrentContext()

    // Set the quality level to use when rescaling
    context!.interpolationQuality = CGInterpolationQuality.default
    let flipVertical = CGAffineTransform(a: 1, b: 0, c: 0, d: -1, tx: 0, ty: newSize.height )
    context!.concatenate(flipVertical)

    // Draw into the context; this scales the image
    context?.draw(image.cgImage!, in: CGRect(x: 0.0,y: 0.0, width: newRect.width, height: newRect.height))

    let newImageRef = context!.makeImage()! as CGImage
    let newImage = UIImage(cgImage: newImageRef)

    // Get the resized image from the context and a UIImage
    UIGraphicsEndImageContext()

    return newImage
}

I have already set the content mode of image to Aspect Fit but still it is not working.

This is how I called the above code in my collection view controller

 cell.imageView.image =  UIImage(named: dogImages[indexPath.row])?.resizeImage(image: UIImage(named: dogImages[indexPath.row])

I manually selected my image in storyboard and set its content mode to apsect fit

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Coder221
  • 1,403
  • 2
  • 19
  • 35

2 Answers2

0

Did you tried setting the newSize in aspect ratio of original Image size. If you want width fix calculate the height as per width and if you want height fix then calculate width as per height

Calculate height when width is fix:

    let fixedWidth: CGFloat = 200
    let newHeight = fixedWidth * image.size.height / image.size.width
    let convertedImage = resizeImage(image: image, newSize: CGSize(width: fixedWidth, height: newHeight))

Calculate width when height is fix:

    let fixedheight: CGFloat = 200
    let newWidth = fixedheight * image.size.width / image.size.height
    let convertedImage = resizeImage(image: image, newSize: CGSize(width: newWidth, height: fixedheight))

You can use this resized image with aspect fit ratio.

also check the answer: https://stackoverflow.com/a/8858464/2677551, that may help

Van
  • 1,225
  • 10
  • 18
0
  func scaleImageAspectFit(newSize: CGSize) -> UIImage? { 

            var scaledImageRect: CGRect = CGRect.zero

            let aspectWidth: CGFloat = newSize.width / size.width
            let aspectHeight: CGFloat = newSize.height / size.height
            let aspectRatio: CGFloat = min(aspectWidth, aspectHeight)

            scaledImageRect.size.width = size.width * aspectRatio
            scaledImageRect.size.height = size.height * aspectRatio

            scaledImageRect.origin.x = (newSize.width - scaledImageRect.size.width) / 2.0
            scaledImageRect.origin.y = (newSize.height - scaledImageRect.size.height) / 2.0

            UIGraphicsBeginImageContextWithOptions(newSize, false, 0)
            if UIGraphicsGetCurrentContext() != nil {
              draw(in: scaledImageRect)
              let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
              UIGraphicsEndImageContext()

              return scaledImage
            }
            return nil
          }

Usage :

let resizedImage = oldImage.scaleImageAspectFit(newSize: CGSize(width: nexSize.width, height: nexSize.height))
Nikhil Viradiya
  • 87
  • 2
  • 15