当 ContentMode = AspectFill 时在 UIImageView 中获取 UImage 的大小

Get Size of UImage when in UIImageView when ContentMode = AspectFill

我目前正在编辑带有核心图形的 UIImage

UIImage内容模式设置为.aspectFill

当我在图像上绘图时,我想保持图像的比例/比例。

我尝试了以下代码,但它 returns .aspectFit 值而不是 .AspectFill

let imageSize = AVMakeRect(aspectRatio: (mainImageView.image?.size)!, insideRect: mainImageView.frame)

如有任何帮助,我们将不胜感激

托马斯

该函数只能进行纵横比匹配。

这是一个将执行纵横比填充的函数:

func makeFillRect(aspectRatio: CGSize, insideRect: CGRect) -> CGRect {
    let aspectRatioFraction = aspectRatio.width / aspectRatio.height
    let insideRectFraction = insideRect.size.width / insideRect.size.height
    let r: CGRect
    if (aspectRatioFraction > insideRectFraction) {
        let w = insideRect.size.height * aspectRatioFraction
        r = CGRect(x: (insideRect.size.width - w)/2, y: 0, width: w, height: insideRect.size.height)
    } else {
        let h = insideRect.size.width / aspectRatioFraction
        r = CGRect(x: 0, y: (insideRect.size.height - h)/2, width: insideRect.size.width, height: h)
    }
    return r
}