7

I'm trying to get an image of the entire screen as the user sees it. The following code should work however it will only work in the simulator. What do I have to do to get this to work on a device running iOS 8.1?

UIView *snapshot = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
CGSize outputSize = CGSizeMake([UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
UIGraphicsBeginImageContextWithOptions(outputSize, NO, 0);
[snapshot drawViewHierarchyInRect:CGRectMake(0.0, 0.0, outputSize.width, outputSize.height) afterScreenUpdates:YES];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Happy to try alternative strategies but the key is I want it as the user sees it - keyboards and all.

Cheers

Kieran Harper
  • 1,098
  • 11
  • 22

1 Answers1

1
UIGraphicsBeginImageContextWithOptions(CGSizeMake(CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)), NO, 1.0f);
        [self.view drawViewHierarchyInRect:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)) afterScreenUpdates:NO];
        UIImage *snapshotImage = UIGraphicsGetImageFromCurrentImageContext();
Hardik Kardani
  • 576
  • 3
  • 24
  • Unfortunately it isn't that simple. I want the whole screen as the user sees it - this renders a view which means even if you use the root view you'll still be missing the status bar and keyboard (hence I'm trying to use UIScreen's method). – Kieran Harper Jan 06 '15 at 21:41
  • For those Googling, this fixed the black screenshot issue I was having with WKWebView. – ninjaneer Apr 17 '15 at 08:44