iPhone 快照包括键盘 [英] iPhone snapshot including keyboard

查看:105
本文介绍了iPhone 快照包括键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找对包括键盘在内的整个 iPhone 屏幕进行快照的正确方法.

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();

但它也不会捕获键盘.看起来 keyWindow 不包括键盘视图.

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

顺便说一句,我需要 UIImage 作为最终结果,而不是 UIView,所以我不能使用其他快照 API.

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?

推荐答案

键盘被放置在其他窗口中.与在iOS9 中放置的键盘附件不同.所以最好的解决方案是渲染所有窗口.

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

这是工作代码:

- (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;
}

这篇关于iPhone 快照包括键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆