将 UIView 转换为 UIImage

Method One

1
2
3
4
5
6
7
8
let image: UIImage?
let scale = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, scale)
if let context = UIGraphicsGetCurrentContext() {
view.layer.render(in: context)
}
image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Method Two

1
2
3
4
let render = UIGraphicsImageRenderer(size: view.bounds.size)
let image = render.image { context in
view.layer.render(in: context.cgContext)
}