[英]Frame and Image in specific aspect Ratio
我使用下面的代码创建了空的灰色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!
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.