我目前正在编辑具有核心图形的UIImage
。UIImage 内容模式设置为 .aspectFill
当我在图像上绘制时,我想保持图像的比例/比例。
我尝试了以下代码,但它返回 .aspectFit 值而不是 .方面填充
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
}