首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIAlertView截图

UIAlertView截图
EN

Stack Overflow用户
提问于 2014-05-31 15:19:34
回答 3查看 931关注 0票数 0

我需要得到我的应用程序的屏幕截图,当有一个UIAlertView礼物。在iOS7中我使用

代码语言:javascript
复制
[[self getMainWindow] drawViewHierarchyInRect:[[UIScreen mainScreen] bounds] afterScreenUpdates:NO];

问题是,图像变成了尴尬的颜色和奇怪的行为。我得到的图像是坏的-跟随是我的形象。

我尝试过许多不同的东西,但没有成功地把颜色和形象弄对。

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

谢谢!

编辑:

实际上,这个问题与renderInContext之前的drawViewHierarchyInRect有关。如果我删除它,图像就是警报视图本身,而不是它下面的视图。如果我把渲染放在它之前,图像就是结果。任何想法都会受到高度赞赏。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-06-01 07:20:43

通过捕获UIAlertView窗口本身,我成功地实现了这一目标。Iv'e忽略了drawViewHierarchyInRect而支持renderInContext,并以不同的方法呈现UIAlertView窗口:

代码语言:javascript
复制
CGContextSaveGState(UIGraphicsGetCurrentContext());
// Center the context around the window's anchor point
CGContextTranslateCTM(UIGraphicsGetCurrentContext(), [alertView center].x, [alertView center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(UIGraphicsGetCurrentContext(), [alertView transform]);

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

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

// Restore the context
[[alertView layer] renderInContext:UIGraphicsGetCurrentContext()];

CGContextRestoreGState(UIGraphicsGetCurrentContext());

通过向UIWindowDidBecomeVisibleNotification注册并根据窗口前缀获取警报窗口,我得到了正确的窗口-- IOS6IOS7之间的不同。

另一个重要问题是主要窗口。显然,在IOS7中,当显示一个UIAlertView时,它就变成了主窗口,所以当试图呈现主窗口时,您只会得到警告视图,而不会看到它下面的视图。因此,为了获得正确的主窗口,我修改了getMainWindow方法以符合以下情况:

代码语言:javascript
复制
- (UIWindow *)getMainWindow
{
    UIWindow *topWndow = [UIApplication sharedApplication].keyWindow;
    UIViewController *topController = topWndow.rootViewController;

    if (topController == nil || [[topWndow description] hasPrefix:@"<_UIModalItemHostingWin"])
    {
        // The windows in the array are ordered from back to front by window level; thus,
        // the last window in the array is on top of all other app windows.
        for (UIWindow *aWndow in [[UIApplication sharedApplication].windows reverseObjectEnumerator])
        {
            if (![[aWndow description] hasPrefix:@"<_UIModalItemHostingWin"])
            {
                topController = aWndow.rootViewController;
                if (topController)
                    return aWndow;
            }
        }
    }

    return topWndow;
}

现在,我有了一个适当的屏幕截图,并对ios6 6/7都有警觉的视图。

票数 2
EN

Stack Overflow用户

发布于 2014-11-07 10:20:10

我无法在屏幕上捕捉到UIAlertview和键盘。

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

在这呢!

代码语言:javascript
复制
- (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;
}
票数 5
EN

Stack Overflow用户

发布于 2014-06-01 01:06:40

我不知道您想要的是图像还是快照视图,但如果后者,拍摄屏幕快照可以获得警报视图和背景。作为测试,在创建快照之后,我取消了警报并将快照视图添加为子视图,并且从“实时”警报切换到快照视图是无缝的。

代码语言:javascript
复制
@interface ViewController ()
@property (strong,nonatomic) UIAlertView *alert;
@property (strong,nonatomic) UIView *aView;
@end

@implementation ViewController

- (IBAction)showAlert:(UIButton *)sender {
    self.alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"This is my message to test the alert view" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
    [self.alert show];
}


-(void)didPresentAlertView:(UIAlertView *)alertView {
    self.aView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
    [self.alert dismissWithClickedButtonIndex:0 animated:NO];
    [self.view addSubview:self.aView];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23971636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档