[英]objc Record Screenshot on iPad - Works on simulator but not on devie
I try to record a screenshot programatically. 我尝试以编程方式记录屏幕截图。 On the simulator the code snipped works like a charm, but on the iPad it only returns a blank page: 在模拟器上,被截断的代码就像一个超级按钮,但在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();
The following line should return a view (including the statusbar). 以下行应返回一个视图(包括状态栏)。 It works on the simulator but on a real device this view is empty. 它可以在模拟器上运行,但在实际设备上,此视图为空。
UIView* screenshotView = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
What' the reason ? 是什么原因 Thanks in advance. 提前致谢。
如果要获取UIImage对象,则不调用snapshotViewAfterScreenUpdates API,只需在keyWindow上调用drawViewHierarchyInRect API,但是如果希望已使用图像绘制视图,则调用snapshotViewAfterScreenUpdates,则无需调用drawViewHierarchyInRect API
For anybody who is looking for the correct answer: 对于正在寻找正确答案的任何人:
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();
The answer is taken from here - with the solution above you are allowed to capture the statusbar as well. 答案是从这里获取的 -使用上述解决方案,您还可以捕获状态栏。
PS: check the code for the right orientation and size of the screenshot ... PS:检查代码的正确方向和屏幕截图的大小...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.