Screenshot without status bar

I am trying to take a screenshot using this code:

- (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor lightGrayColor]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidAppear:(BOOL)animated{ UIImageWriteToSavedPhotosAlbum([self screenshot], self, @selector(image: didFinishSavingWithError:contextInfo:), nil); } - (UIImage*)screenshot { // Create a graphics context with the target size // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext CGSize imageSize = [[UIScreen mainScreen] bounds].size; if (NULL != UIGraphicsBeginImageContextWithOptions) UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); else UIGraphicsBeginImageContext(imageSize); CGContextRef context = UIGraphicsGetCurrentContext(); // Iterate over every window from back to front for (UIWindow *window in [[UIApplication sharedApplication] windows]) { if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { // -renderInContext: renders in the coordinate space of the layer, // so we must first apply the layer geometry to the graphics context CGContextSaveGState(context); // Center the context around the window anchor point CGContextTranslateCTM(context, [window center].x, [window center].y); // Apply the window transform about the anchor point CGContextConcatCTM(context, [window transform]); // Offset by the portion of the bounds left of and above the anchor point CGContextTranslateCTM(context, -[window bounds].size.width * [[window layer] anchorPoint].x, -[window bounds].size.height * [[window layer] anchorPoint].y); // Render the layer hierarchy to the current context [[window layer] renderInContext:context]; // Restore the context CGContextRestoreGState(context); } } // Retrieve the screenshot image UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image; } 

But the saved image has a status bar in which the signal is applied, including the signal, time and battery. How can I take a screenshot including the contents of the status bar? enter image description here

+4
source share
3 answers

Hide the status bar before taking a screenshot as follows:

([[UIApplication sharedApplication] setStatusBarHidden: YES];)

+1
source

(Tested with iOS 7 and autoload)

I use the screenshot function of the simulator in conjunction with some compilation of conditional code. By uncommenting #define , I can quickly switch to "take snapshot" mode for the startup screen

The main trick is to hide the status bar when the view controller appears on your home screen, but with a very long animation, so you have enough time to click on the screenshot command in the simulator, which makes the screenshot right on your desktop (or take a screenshot on the device if you want to).

Using the code below, the status bar immediately disappears thanks to " UIStatusBarAnimationNone ", but the "sliding screen" is animated for a very long period (5000.0 seconds in the code below). Therefore, before the bar begins to “move” 1 point of 20 points vertically, you will receive approximately 5000/20 = 250 seconds to take a screenshot (and coffee).

 - (BOOL)prefersStatusBarHidden { return YES; } - (UIStatusBarAnimation)preferredStatusBarUpdateAnimation { #ifdef kTAKE_LAUNCHIMAGE_SCREENSHOT return UIStatusBarAnimationNone; #else return UIStatusBarAnimationSlide; #endif } - (void)removeStatusBarAnimated { #ifdef kTAKE_LAUNCHIMAGE_SCREENSHOT [UIView animateWithDuration:5000.0 animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }]; #else [UIView animateWithDuration:2.0 animations:^{ [self setNeedsStatusBarAppearanceUpdate]; }]; #endif } 

for more information on status bar control in iOS 7, check out my answer and code here:

fooobar.com/questions/175993 / ...

+1
source

This solution does not hide the status bar, just use the correct borders and note that the offset y must be negative, here the "map" is the view that fills the screen below the status bar: (The code is written inside the ViewController)

 UIGraphicsBeginImageContextWithOptions(self.map.bounds.size, NO, [UIScreen mainScreen].scale); CGRect bounds = CGRectMake(0, -1*(self.view.bounds.size.height-self.map.bounds.size.height), self.map.bounds.size.width, self.map.bounds.size.height); BOOL success = [ self.view drawViewHierarchyInRect:bounds afterScreenUpdates:YES]; UIImage* image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
0
source

All Articles