With UIGraphicsImageRenderer.

let image = UIGraphicsImageRenderer(bounds: view.bounds).image { context in
    view.layer.render(in: context.cgContext)
}