Search code examples
iosscreenshotuipickerview

UIPickerView get darker on screenshot


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?


Solution

  • 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.