Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone snapshot including keyboard

I'm looking for the proper way to snapshot the entire iPhone screen including the keyboard.

i found some code to snapshot the screen:

CGRect screenCaptureRect = [UIScreen mainScreen].bounds;
UIView *viewWhereYouWantToScreenCapture = [[UIApplication sharedApplication] keyWindow];
//screen capture code
UIGraphicsBeginImageContextWithOptions(screenCaptureRect.size, NO, [UIScreen mainScreen].scale);
[viewWhereYouWantToScreenCapture drawViewHierarchyInRect:screenCaptureRect afterScreenUpdates:NO];
UIImage *capturedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

but it doesn't capture the keyboard as well. it looks like the keyWindow doesn't include the keyboard view.

btw I need UIImage as final result, not UIView, so I can't use other snapshot API's.

Any ideas of to do that and with best performance?

like image 917
Eyal Avatar asked Dec 14 '22 17:12

Eyal


1 Answers

Keyboard is placed in other window. And in iOS9 keyboard accessory placed in different one. So the best solution is to render all windows.

Here is working code:

- (UIImage*)fullScreenShot {
    CGSize imgSize = [UIScreen mainScreen].bounds.size;
    UIGraphicsBeginImageContextWithOptions(imgSize, NO, 0);
    for (UIWindow* window in [UIApplication sharedApplication].windows) {
        if (![window respondsToSelector:@selector(screen)] || window.screen == [UIScreen mainScreen]) {
            [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
        }
    }
    UIImage* img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return img;
}
like image 141
zxcat Avatar answered Dec 31 '22 12:12

zxcat