【问题标题】:Resizing the image to Aspect Fit将图像大小调整为 Aspect Fit
【发布时间】:2017-10-11 01:13:48
【问题描述】:

我正在尝试使用以下流行代码调整图像大小,它正在调整图像大小,但它正在将图像大小调整为 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

【问题讨论】:

标签: ios swift core-graphics


【解决方案1】:

您是否尝试将 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,这可能会有所帮助

【讨论】:

    【解决方案2】:
      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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-17
      • 2016-12-30
      • 2019-04-18
      • 2013-08-12
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多