问题描述
我有一个大小为1080x1920的UIImage和一个大小为375x812的UIView。 我想按这种方式放大视图并将其绘制在中心的UIImage上:
这是我实现的通过UIImage放大UIView的代码:
let proportionalWidthUpperView = (inImage.size.height * self.view.frame.size.width)/self.view.frame.size.height
let differenceWidth = inImage.size.width - proportionalWidthUpperView
UIGraphicsBeginImageContext(inImage.size)
let rect = CGRect(x: differenceWidth/2,y: 0,width: inImage.size.width-(differenceWidth),height: inImage.size.height)
inImage.draw(in: CGRect(x: 0,width: inImage.size.width,height: inImage.size.height))
imageFromUpperView?.draw(in: rect)
let result = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return result!
按比例放大视图以使其与UIImage高度匹配,我检索宽度差并将其添加到矩形的左右。
此方法在iPhone上效果很好,但是在更大视野的iPad上使用时,我将视图放大了,您知道我在做什么吗?
解决方法
尝试将UIView作为子视图添加到UIImageView,您可以应用转换和缩放并保留添加的UIView框架。
希望这会简化并提供帮助。