1

我有一个 UIView 和一个 UITableView 在键盘下方延伸。表格视图中的内容足够亮,清楚地表明内容位于键盘后面。我正在尝试截取整个视图的屏幕截图,以便使用以下代码对其进行模糊处理:

- (UIImage *)screenshotFromView:(UIView *)view;
{
    UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, 0.0);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

但是,返回的图像不会创建透明键盘。当从非模糊视图转到模糊视图时,这会出现奇怪的过渡,因为在过渡到模糊图像之前,键盘后面有明显的内容。

是否可以在不使用私有API的情况下截取整个屏幕,同时仍然保持键盘+状态栏的透明度?

4

2 回答 2

2

这些天我遇到了和你一样的问题,所以我完全知道你想要什么。我希望整个 UI 模糊在一条消息后面,包括键盘,这不包含在任何常规的屏幕截图方法中。我的治疗方法是以下代码:

- (UIImage*)screenShotWithKeyboard:(UIView *)viewToShoot
{
    UIWindow *keyboard = nil;
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if ([[window description] hasPrefix:@"<UITextEffectsWin"])
        {
            keyboard = window;
            break;
        }
    }

    // Define the dimensions of the screenshot you want to take (the entire screen in this case)
    CGSize size =  [[UIScreen mainScreen] bounds].size;

    // Create the screenshot
    UIGraphicsBeginImageContext(size);

    CGContextRef context=UIGraphicsGetCurrentContext();

    // delete following line if you only want the keyboard
    [[viewToShoot layer] renderInContext:context];

    if(keyboard!=nil)
        [[keyboard layer] renderInContext:context];

    UIImage *screenImg = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return screenImg;
}

我从Aran Balkan的一篇文章中得到了这个想法,我将它分解为一种方法,并为 iOS 7 测试它,这似乎对我有用。这篇文章值得一读,因为他稍微解释了背后的技巧。由于您只想要实际的键盘作为图像,您可以注释掉我在代码中标记的行。使用该图像键盘,您可以自己进行模糊处理。代码远非完美,但我认为你明白了。

最后两个想法:我是objective c和iOS开发的大一新生。如果您发现任何有问题的错误,非常欢迎发表评论以改进此答案。其次,我今天在我的应用程序中编写了这段代码,我还不知道我是否违反了任何 iOS 开发者规则。目前我没有看到任何问题,但我会进一步调查,因为我想用那个图形技巧发布我的应用程序。我会不断更新这篇文章。在此之前,与第一点一样,我非常感谢您对这个问题发表任何评论。

于 2013-11-22T18:58:30.053 回答
0

你考虑过使用UIKeyboardAppearanceDark吗?当前的默认值keyboardAppearance对应于UIKeyboardAppearanceLight,因此它可能不适合您的用例。

于 2013-11-22T11:20:57.893 回答