在iPad上的objc记录截图 - 在模拟器上工作但在devie上没有

时间:2015-07-10 11:57:41

标签: objective-c ios7 uiview

我尝试以编程方式录制屏幕截图。在模拟器上,剪辑的代码就像魅力一样,但在iPad上它只返回一个空白页面:

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
UIGraphicsBeginImageContextWithOptions(keyWindow.frame.size, NO, 0);

UIView* screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];

[screenshotView drawViewHierarchyInRect:keyWindow.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

以下行应返回视图(包括状态栏)。它适用于模拟器,但在真实设备上,此视图为空。

UIView* screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];

是什么原因?提前致谢。

2 个答案:

答案 0 :(得分:0)

如果你想获取UIImage对象不要调用snapshotViewAfterScreenUpdates API,只需在keyWindow上调用drawViewHierarchyInRect API,但是如果你想要用图像绘制的视图然后调用snapshotViewAfterScreenUpdates,你就不需要调用drawViewHierarchyInRect了。 API

答案 1 :(得分:0)

对于任何正在寻找正确答案的人:

NSMutableArray* windows = [[[UIApplication sharedApplication] windows] mutableCopy];

NSString *statusBarString = [NSString stringWithFormat:@"_statusBarWindow"];
UIWindow* statusBarWindow =  [[UIApplication sharedApplication] valueForKey:statusBarString];

/*CGRect statusBarFrame = statusBarWindow.frame;
statusBarFrame.origin.x -= 20;
statusBarFrame.origin.y -= 20;
[statusBarWindow setFrame:statusBarFrame];*/
//statusBarWindow.backgroundColor = [UIColor blackColor];
[windows addObject:statusBarWindow];
[statusBarWindow release];



UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
for (UIWindow *window in windows) {
    CGContextSaveGState(context);
    CGContextTranslateCTM(context, window.center.x, window.center.y);
    CGContextConcatCTM(context, window.transform);
    CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);
    if (orientation == UIInterfaceOrientationLandscapeLeft) {
        CGContextRotateCTM(context, M_PI_2);
        if(![window isKindOfClass:[UIStatusBarWindow class]])
        {
            CGContextTranslateCTM(context, 20, -imageSize.width+20);
        }
        else
        {
            CGContextTranslateCTM(context, 0, -imageSize.width);
        }
    } else if (orientation == UIInterfaceOrientationLandscapeRight) {
        CGContextRotateCTM(context, -M_PI_2);
        if(![window isKindOfClass:[UIStatusBarWindow class]])
        {
            CGContextTranslateCTM(context, -imageSize.height+20, 20);
        }
        else
        {
            CGContextTranslateCTM(context, -imageSize.height, 0);
        }
    } else if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
        CGContextRotateCTM(context, M_PI);
        if(![window isKindOfClass:[UIStatusBarWindow class]])
        {
            CGContextTranslateCTM(context, -imageSize.width+20, -imageSize.height+20);
        }
        else
        {
            CGContextTranslateCTM(context, -imageSize.width, -imageSize.height);
        }
    }
    if ([window respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];
    } else {
        [window.layer renderInContext:context];
    }
    CGContextRestoreGState(context);
}

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

答案取自here - 通过上述解决方案,您也可以捕获状态栏。

PS:检查代码的正确方向和屏幕大小...