This is the extension we used to capture UIView
into UIImageView
. The solution doesn't contain any usage of UIReplicatView
as you suggested. But it has been working great for us:
extension UIView {
// Note: only return the image after viewDidAppear called
func takeScreenShot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(bounds.size, isOpaque, UIScreen.main.scale)
if let scrollView = self as? UIScrollView {
let offset = scrollView.contentOffset
UIGraphicsGetCurrentContext()?.translateBy(x: -offset.x, y: -offset.y);
}
drawHierarchy(in: bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image
}
}