A blog about Swift and iOS tricks

Render a UIView instance as an Image

Converting a UIView to an image and save it is a common practice for many of us. Here is a convenient way to do that.

extension UIView {
    func snapshot() -> UIImage {
        let renderer = UIGraphicsImageRenderer(bounds: bounds)
        return renderer.image { action in
            layer.render(in: action.cgContext)
        }
    }
}