我目前正在使用这种非常受欢迎的方法来捕捉视图,以便用户分享或将其保存到他的照片库:
let renderer = UIGraphicsImageRenderer(size: CGSize(width: self.viewToCapture.bounds.size.width, height: self.viewToCapture.bounds.size.height))
let image = renderer.image { ctx in
self.viewToCapture.drawHierarchy(in: self.viewToCapture.bounds, afterScreenUpdates: true)
}
但问题的结果是非常不满意,因为图像的图像分辨率不是很好。到目前为止,我还没有找到另一种方法。渲染器没有任何可以调整渲染图像质量的属性(或者至少我知道没有)。
所以,我正在寻找上面显示的修改或者捕获视图的另一种方式,并且结果非常清晰。如果您能分享您对该问题的了解,我们将非常高兴。提前谢谢。
答案 0 :(得分:2)
我使用此扩展程序从视图创建图像 UIGraphicsGetCurrentContext()返回对当前图形上下文的引用。它没有创建一个。这一点很重要,因为如果您在该灯光中查看它,您会发现它不需要大小参数,因为当前上下文只是创建图形上下文的大小。
extension UIView {
func toImage() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}