Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capturing full screenshot with status bar in iOS programmatically

I am using this code to capture a screenshot and to save it to the photo album.

-(void)TakeScreenshotAndSaveToPhotoAlbum
{
   UIWindow *window = [UIApplication sharedApplication].keyWindow;

   if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
       UIGraphicsBeginImageContextWithOptions(window.bounds.size, NO, [UIScreen mainScreen].scale);
   else
       UIGraphicsBeginImageContext(window.bounds.size);

   [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

   UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

But the problem is whenever the screenshot is saved, I see the status bar of iPhone is not captured. Instead a white space appears at the bottom. Like the following image: enter image description here

What am I doing wrong?

like image 596
Umair Khan Jadoon Avatar asked Dec 14 '11 16:12

Umair Khan Jadoon


People also ask

How do I take a full length screenshot on my iPhone?

You can take a full-page, scrolling screenshot of a webpage, document, or email that exceeds the length of your iPhone screen, then save it as a PDF. Do one of the following: On an iPhone with Face ID: Simultaneously press and then release the side button and volume up button.

Does IOS have long screenshot?

Now press the side button (power key) and volume up button simultaneously on your iPhone. It will capture the screenshot, you can see the preview in the bottom left of your iPhone's screen. Tap on the screenshot preview window, to turn into a long screenshot. Then tap on the Full Page option from the preview window.


2 Answers

The status bar is actually in its own UIWindow, in your code you are only rendering the view of your viewcontroller which does not include this.

The "official" screenshot method was here but now seems to have been removed by Apple, probably due to it being obsolete.

Under iOS 7 there is now a new method on UIScreen for getting a view holding the contents of the entire screen:

- (UIView *)snapshotViewAfterScreenUpdates:(BOOL)afterUpdates

This will give you a view which you can then manipulate on screen for various visual effects.

If you want to draw the view hierarchy into a context, you need to iterate through the windows of the application ([[UIApplication sharedApplication] windows]) and call this method on each one:

- (BOOL)drawViewHierarchyInRect:(CGRect)rect afterScreenUpdates:(BOOL)afterUpdates

You may be able to combine the two above approaches and take the snapshot view, then use the above method on the snapshot to draw it.

like image 82
jrturton Avatar answered Sep 28 '22 06:09

jrturton


The suggested "official" screenshot method doesn't capture status bar (it is not in the windows list of the application). As tested on iOS 5.

I believe, this is for security reasons, but there is no mention of it in the docs.

I suggest two options:

  • draw a stub status bar image from resources of your app (optionally update time indicator);
  • capture only your view, without status bar, or trim image afterwards (image size will differ from standard device resolution); status bar frame is known from corresponding property of application object.
like image 29
paiv Avatar answered Sep 28 '22 04:09

paiv