title: How to save screenshot of a view with its subviews in Swift?
body: <p>I want to capture a view with its subviews. I'm using this code to capture the screen:</p>
  
  <pre><code>let view = self.faceTrackerContainerView
  
  UIGraphicsBeginImageContext(CGSizeMake(view.bounds.size.width, view.bounds.size.height))
  UIGraphicsGetCurrentContext()!
  self.view?.drawViewHierarchyInRect(view.frame, afterScreenUpdates: true)
  let screenshot = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  
  UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
  </code></pre>
  
  <p>These codes capture the whole screen including a button that's in front of the view. I tried these codes but it only captures the main view excluding its subviews:</p>
  
  <pre><code>let view = self.faceTrackerContainerView
  let scale = UIScreen.mainScreen().scale
  UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, scale);
  
  view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
  let screenshot = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  
  UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
  </code></pre>
  
  <p>I also tried the <code>snapshotViewAfterScreenUpdates(true)</code> from here but I don't know how to implement it. I have a collectionView below the view and a button on top of the view (both are not in the view). The first codes capture everything with the button, collectionView, and subviews. The second codes capture the view without the subviews. What am I missing?</p>