Screenshot from UIView

Swift, iOS, UIKit

Generate a screenshot from a UIView

This extension will generate a UIImage screenshot from a view, that can be used for sharing for example.


extension UIView { var screenshot: UIImage? { UIGraphicsBeginImageContextWithOptions(bounds.size, true, UIScreen.main.scale) UIGraphicsGetCurrentContext() layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() return image } }
let screenshot = someView.screenshot