UIAlertView 截图

Ada*_*dam 0 rendering uialertview ios

当有UIAlertView礼物时,我需要获取我的应用程序的屏幕截图。在 iOS7 中我使用

[[self getMainWindow] drawViewHierarchyInRect:[[UIScreen mainScreen] bounds] afterScreenUpdates:NO];
Run Code Online (Sandbox Code Playgroud)

问题是图像变得带有尴尬的颜色和奇怪的行为。我得到的图像很糟糕 - 以下是我的图像。

在此处输入图片说明

我尝试了很多不同的东西,但都没有成功获得正确的颜色和图像。

在 iOS6 中,我根本没有找到捕获的方法UIAlertView。实现这一目标的最佳方法是什么?

谢谢!

编辑:

实际上问题与renderInContext之前有关drawViewHierarchyInRect。如果我删除它,图像是警报视图本身,没有它下面的视图。如果我在它之前保留渲染,则图像就是结果。任何想法都将受到高度赞赏。

Bhu*_*ani 5

我无法在屏幕截图中捕获 UIAlertview 和键盘。

我创建了一个函数,可以帮助捕获窗口所有元素的屏幕截图。

这里是!

- (UIImage *)screenshot
{
    UIWindow *win = appDelegate.window;

    // Also checking for version directly for 3.2(.x) since UIGraphicsBeginImageContextWithOptions appears to exist
    // but can't be used.
    float systemVersion = [[[UIDevice currentDevice] systemVersion] floatValue];

    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    if (systemVersion >= 4.0f)
    {
        UIGraphicsBeginImageContextWithOptions(win.frame.size, NO, 0);

    } else {
        UIGraphicsBeginImageContext(win.frame.size);
    }

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    //NSInteger count = 0;

    NSMutableArray *arrAllWindow = [[NSMutableArray alloc] init];
    [arrAllWindow addObjectsFromArray:[[UIApplication sharedApplication] windows]]; // This will allow keyboard capturing

    //below code will allow UIAlertview capturing
    if (IS_Ios8)
    {
        if ([[[UIApplication sharedApplication].keyWindow description] hasPrefix:@"<_UIAlertControllerShimPresenterWin"])
        {
            [arrAllWindow addObject:[UIApplication sharedApplication].keyWindow];
        }
    }
    else
    {
        if ([[[UIApplication sharedApplication].keyWindow description] hasPrefix:@"<_UIModalItemHostingWin"])
        {
            [arrAllWindow addObject:[UIApplication sharedApplication].keyWindow];
        }
    }

    for (int i=0;i<arrAllWindow.count; i++)
    {
        UIWindow *window = [arrAllWindow objectAtIndex:i];
        // -renderInContext: renders in the coordinate space of the layer,
        // so we must first apply the layer's geometry to the graphics context
        CGContextSaveGState(context);
        // Center the context around the window's anchor point
        CGContextTranslateCTM(context, [window center].x, [window center].y);
        // Apply the window's transform about the anchor point
        CGContextConcatCTM(context, [window transform]);

        // Y-offset for the status bar (if it's showing)
        NSInteger yOffset = [UIApplication sharedApplication].statusBarHidden ? 0 : -20;

        // Offset by the portion of the bounds left of and above the anchor point
        CGContextTranslateCTM(context,
                              -[window bounds].size.width * [[window layer] anchorPoint].x,
                              -[window bounds].size.height * [[window layer] anchorPoint].y + yOffset);

        [window drawViewHierarchyInRect:win.frame afterScreenUpdates:YES];

        // Restore the context
        CGContextRestoreGState(context);
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}
Run Code Online (Sandbox Code Playgroud)