4

I had to alter the navigation on certain circumstances, and due to complexity of the transitions I had take an paint and screenshot until the transition is finished. In almost cases, that works pretty well, but there is a point that disturb me. I have a view controller with two picker views:

enter image description here

But the screenshot is not working well on this VC. I get this:

enter image description here

The code that takes the screenshot is the following in both cases:

- (UIImage *)takeScreenshot {
    CALayer *layer = [[UIApplication sharedApplication] keyWindow].layer;
    UIGraphicsBeginImageContext(layer.frame.size);
    [layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    return screenshot;
}

Anyone knows how could be happened?

jherran
  • 3,337
  • 8
  • 37
  • 54
  • [layer renderInContext:UIGraphicsGetCurrentContext()]; remove this line and try. – Ashok Londhe May 04 '15 at 12:41
  • Your solution does not work as intended. If I remove that line, the screenshot didn't display the navigation, not other views, etc. – jherran May 04 '15 at 12:45
  • you r doing some thing wrong.. i will check it wait. – Ashok Londhe May 04 '15 at 12:50
  • If you save your screenshot as a PNG file and open it in an image editor, is it dark? Or is it only dark when you use it in your application (that would mean that you display it incorrectly and not you capture incorrectly). – Nicolas Buquet May 04 '15 at 13:18
  • If I save the image and open with an editor, the image is dark. – jherran May 05 '15 at 09:49

2 Answers2

4

You could try to use a different method for screenshot. Apple introduced in iOS 7 some methods for fast view screenshot.

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *im = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Here is an answer from Apple that provides more info on how the 2 methods works. While the respective user encountered some pb and was advised to use the old way of snapshotting the view, I never had any problem with it. Maybe they fixed it since then.

Community
  • 1
  • 1
pteofil
  • 4,133
  • 17
  • 27
  • Perfect. I had to change `self.view` with `[[UIApplication sharedApplication] keyWindow]` to capture the NavigationBar, otherwise it won't appear. But works great. – jherran May 08 '15 at 06:41
1
UIGraphicsBeginImageContext(self.window.bounds.size);

[self.window.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

NSData * data = UIImagePNGRepresentation(image);

[data writeToFile:@"image.png" atomically:YES];

if you have a retina display then replace the first line with the below code:-

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

else

UIGraphicsBeginImageContext(self.window.bounds.size);
Faran Ghani
  • 277
  • 4
  • 17