【问题标题】:Get Size of UImage when in UIImageView when ContentMode = AspectFill当 ContentMode = AspectFill 时在 UIImageView 中获取 UImage 的大小
【发布时间】:2017-04-13 18:35:36
【问题描述】:
我目前正在编辑带有核心图形的 UIImage
UIImage 内容模式设置为 .aspectFill
当我在图像上绘画时,我想保持图像的比例/比例。
我尝试了以下代码,但它返回 .aspectFit 值而不是 .AspectFill
let imageSize = AVMakeRect(aspectRatio: (mainImageView.image?.size)!, insideRect: mainImageView.frame)
任何帮助将不胜感激
托马斯
【问题讨论】:
标签:
swift
uiimageview
core-graphics
【解决方案1】:
那个函数只能做一个方面拟合。
这是一个将进行方面填充的函数:
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
}