将图像大小调整为Aspect Fit

时间:2017-10-11 01:13:49

标签: ios swift core-graphics

我正在尝试使用以下热门代码调整图片大小,它正在调整图像大小,但它正在调整图像大小为Scale to Fill,我想将它们调整为Aspect Fit.我如何做到这一点?

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
}

我已将图片的内容模式设置为Aspect Fit,但仍然无效。

这就是我在集合视图控制器中调用上面代码的方法

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

我在故事板中手动选择了我的图片,并将其内容模式设置为apsect fit

2 个答案:

答案 0 :(得分:0)

您是否尝试过设置原始图像尺寸的纵横比newSize。如果要宽度修复,请根据宽度计算高度,如果需要高度修复,则按高度计算宽度

计算宽度修正时的高度:

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

计算高度修正时的宽度:

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

您可以将此调整大小的图像与宽高比配合使用。

还要检查答案:https://stackoverflow.com/a/8858464/2677551,这可能会有所帮助

答案 1 :(得分: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
          }

用法:

let resizedImage = oldImage.scaleImageAspectFit(newSize: CGSize(width: nexSize.width, height: nexSize.height))