我正在尝试使用UIView可以放置在任何位置的叠加层来裁剪图像视图的子图像UIImageView.当UIImageView内容模式为'Aspect Fit'时,我正在借用类似帖子中的解决方案来解决这个问题.建议的解决方案是:
func computeCropRect(for sourceFrame : CGRect) -> CGRect {
let widthScale = bounds.size.width / image!.size.width
let heightScale = bounds.size.height / image!.size.height
var x : CGFloat = 0
var y : CGFloat = 0
var width : CGFloat = 0
var height : CGFloat = 0
var offSet : CGFloat = 0
if widthScale < heightScale {
offSet = (bounds.size.height - (image!.size.height * widthScale))/2
x = sourceFrame.origin.x / widthScale
y = (sourceFrame.origin.y - offSet) / widthScale …Run Code Online (Sandbox Code Playgroud)