框架和图像的特定的长宽比
问题描述:
我创建的空灰色的UIImage,使用下面的代码框架和图像的特定的长宽比
let size = CGSize(width: 212, height: 332)
UIGraphicsBeginImageContextWithOptions(size, true, 0)
UIColor.gray.setFill()
UIRectFill(CGRect(x: 0, y: 0, width: size.width, height: size.height))
let backgroundImage2: UIImage? = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
现在我需要把UIImage的特定的区域在该UIImage的。如下图所示。说顶部,左边,右边应该是30像素,底部多于200像素。保持内部图像宽高比。
答
使用两图像视图(或者UIImageView
或GLKView
),使得“图像”中的“灰色背景”视图的子视图。正确定位“图像”后,将两个图像合并为一个。
下面是一个扩展UIView
,我使用:
extension UIView {
public func createImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(
CGSize(width: self.frame.width, height: self.frame.height), true, 1)
self.layer.render(in: UIGraphicsGetCurrentContext()!)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
}
可以使用drawInRect方法来绘制图像,并添加按照规定矩形。那你需要什么? –
@MikeAlter我做到了这一点,但问题是在进行Draw时保持宽高比(in:rect:) –
即使您在最后进行小型演示,您也将面临相同的长宽比问题@MikeAlter –