1

When I take a screenshot to share the current view of my device (iPhone), it only takes the upper part of it, and when I scroll down to the bottom (of my tableview at runtime), the screenshot is blank as if not capturing the current view on the device - I hope I am explaining alright there.

Am I missing anything?

func captureScreen() -> UIImage {

    UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, false, 0);
    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image
}
Laroms
  • 75
  • 1
  • 13
  • Check this http://stackoverflow.com/questions/25448879/how-to-take-full-screen-screenshot-in-swift – Khuong Apr 25 '16 at 15:52
  • Not sure if related, but are you aware of the difference between `bounds` and `frame`? It might be that in one of the methods you should use `frame` instead. – luk2302 Apr 25 '16 at 15:52
  • I think it should be `UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 1)` – Khuong Apr 25 '16 at 15:53
  • It was indeed a case of replacing `bounds` with `frame`. Thank you, guys! Is there a way to accept your answers? I can't see the checkmark. – Laroms Apr 25 '16 at 16:08
  • They are just comments, not answers! Therefore you cannot accept one ;) Glad to help – luk2302 Apr 25 '16 at 16:10
  • Much appreciated :)! – Laroms Apr 25 '16 at 16:14

1 Answers1

-1

Change your code to the following :

func captureScreen() -> UIImage {

UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image

}

Note the change from bound to frame in your code. The last argument in UIGraphicsBeginImageContextWithOptions has to do with the scale. If you specify a value of 0.0, the scale factor is set to the scale factor of the device’s main screen

Learn more about it here

Jonathan Eustace
  • 2,417
  • 12
  • 28
  • 49