Skip to content

Instantly share code, notes, and snippets.

@StanDimitroff
Created February 16, 2018 10:08
Show Gist options
  • Save StanDimitroff/9c1dc069000d9f10b057e74861a3f6bc to your computer and use it in GitHub Desktop.
Save StanDimitroff/9c1dc069000d9f10b057e74861a3f6bc to your computer and use it in GitHub Desktop.
UIImage from UIView
extension UIView {
var asImage: UIImage? {
UIGraphicsBeginImageContextWithOptions(
CGSize(width: self.frame.width, height: self.frame.height), true, 1)
self.layer.render(in: UIGraphicsGetCurrentContext()!)
guard let image = UIGraphicsGetImageFromCurrentImageContext() else { return nil }
UIGraphicsEndImageContext()
return image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment