简体   繁体   English

将图像大小调整为 Aspect Fit

[英]Resizing the image to Aspect Fit

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.我正在尝试使用以下流行代码调整图像大小,它正在调整图像大小,但它将图像大小调整为Scale to Fill ,我想将它们调整为Aspect Fit.大小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.我已经将图像的内容模式设置为Aspect Fit但它仍然无法正常工作。

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我在故事板中手动选择了我的图像并将其内容模式设置为apsect fit

Did you tried setting the newSize in aspect ratio of original Image size.您是否尝试在原始图像大小的纵横比中设置 newSize。 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还要检查答案: https : //stackoverflow.com/a/8858464/2677551 ,这可能会有所帮助

  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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM