Skip to content

Instantly share code, notes, and snippets.

@rickyngan
Created October 12, 2018 07:17
Show Gist options
  • Save rickyngan/15b783744c564d111a509d825f940666 to your computer and use it in GitHub Desktop.
Save rickyngan/15b783744c564d111a509d825f940666 to your computer and use it in GitHub Desktop.
UIView to UIImage
import UIKit
public extension UIView {
@available(iOS 10.0, *)
public func renderToImage(afterScreenUpdates: Bool = false) -> UIImage {
let rendererFormat = UIGraphicsImageRendererFormat.default()
rendererFormat.opaque = isOpaque
let renderer = UIGraphicsImageRenderer(size: bounds.size, format: rendererFormat)
let snapshotImage = renderer.image { _ in
drawHierarchy(in: bounds, afterScreenUpdates: afterScreenUpdates)
}
return snapshotImage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment