Created
October 12, 2018 07:17
-
-
Save rickyngan/15b783744c564d111a509d825f940666 to your computer and use it in GitHub Desktop.
UIView to UIImage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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