How to take a screenshot of all Scrollview content?

I want to create a screenshot of UIScrollView , which should contain all the scroll content, even the content that is not currently displayed to the user. For this, I tried the following two methods:

 func snapShot(view:UIView) -> UIImage { UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0); view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true); let image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } func snapShotScrollView(scrollView:UIScrollView) -> UIImage { let bounds = scrollView.bounds; scrollView.bounds.size = scrollView.contentSize; let image = snapShot(scrollView); scrollView.bounds = bounds; return image; } 

But the resulting image still just displays those view elements inside the scroll view that are currently visible to the user. But I want to see all the opinions.

How can i do this?

EDIT

I also tried:

 func snapshot() -> UIImage? { var image: UIImage? UIGraphicsBeginImageContext(scrollView.contentSize) let savedContentOffset = scrollView.contentOffset let savedFrame = scrollView.frame; scrollView.contentOffset = CGPoint.zero; scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height); scrollView.layer.render(in: UIGraphicsGetCurrentContext()!) image = UIGraphicsGetImageFromCurrentImageContext(); scrollView.contentOffset = savedContentOffset; scrollView.frame = savedFrame; UIGraphicsEndImageContext(); return image } 

Edit 2

My UIScrollView is placed inside a UIView and contains a UIStackView . The view is designed as a popup view so that it looks like a dialog appears. The sample code from my first edit works in an empty UIViewController with only one UIScrollView , but not in the constellation mentioned.

+7
ios swift swift3 uiscrollview screenshot
source share
1 answer

To jump from to this answer by adding a test ViewController:

 class ScreenShotTestViewController: UIViewController { var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() scrollView = UIScrollView(frame: CGRect(origin: CGPoint.zero, size: view.frame.size)) scrollView.contentSize = CGSize(width: view.frame.size.width, height: view.frame.size.height * 2) scrollView.backgroundColor = UIColor.yellow view.addSubview(scrollView) let label = UILabel(frame: CGRect(x: 0.0, y: view.frame.size.height * 1.5, width: view.frame.size.width, height: 44.0)) label.text = "Hello World!" scrollView.addSubview(label) let screenShot = snapshot() } func snapshot() -> UIImage? { UIGraphicsBeginImageContext(scrollView.contentSize) let savedContentOffset = scrollView.contentOffset let savedFrame = scrollView.frame scrollView.contentOffset = CGPoint.zero scrollView.frame = CGRect(x: 0, y: 0, width: scrollView.contentSize.width, height: scrollView.contentSize.height) scrollView.layer.render(in: UIGraphicsGetCurrentContext()!) let image = UIGraphicsGetImageFromCurrentImageContext() scrollView.contentOffset = savedContentOffset scrollView.frame = savedFrame UIGraphicsEndImageContext() return image } } 

Results in full content image including subview:

+6
source share

All Articles