title: How to make a screenshot of all the content of a Scrollview?
body: <p>I want to create a screenshot of a <code>UIScrollView</code> which should contain the whole content of the scroll view, even that content, which is currently not visible to the user. For that I tried the following two methods:</p>
  
  <pre><code>func snapShot(view:UIView) -&gt; UIImage {
      UIGraphicsBeginImageContextWithOptions(view.bounds.size, true, 0);
  
      view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true);
  
      let image = UIGraphicsGetImageFromCurrentImageContext();
  
      UIGraphicsEndImageContext();
  
      return image;
  }
  
  func snapShotScrollView(scrollView:UIScrollView) -&gt; UIImage {
      let bounds = scrollView.bounds;
  
      scrollView.bounds.size = scrollView.contentSize;
  
      let image = snapShot(scrollView);
  
      scrollView.bounds = bounds;
  
      return image;
  }
  </code></pre>
  
  <p>But the resulting image is still just showing those view elements inside the scroll view which are currently visible to the user. But I want to see all views.</p>
  
  <p>How can I do that?</p>
  
  <p><strong>EDIT</strong> </p>
  
  <p>I also tried:</p>
  
  <pre><code>func snapshot() -&gt; 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
  }
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>My <code>UIScrollView</code> is placed inside a <code>UIView</code> and does contain a <code>UIStackView</code>. The View is designed as a popover view so that it looks like a dialogue is popping up. The code sample from my first edit is working in a blank <code>UIViewController</code> with only one <code>UIScrollView</code> but not in the mentioned constellation.</p>