[英]objc Record Screenshot on iPad - Works on simulator but not on devie
我嘗試以編程方式記錄屏幕截圖。 在模擬器上,被截斷的代碼就像一個超級按鈕,但在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];
是什么原因 提前致謝。
如果要獲取UIImage對象,則不調用snapshotViewAfterScreenUpdates API,只需在keyWindow上調用drawViewHierarchyInRect API,但是如果希望已使用圖像繪制視圖,則調用snapshotViewAfterScreenUpdates,則無需調用drawViewHierarchyInRect API
對於正在尋找正確答案的任何人:
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();
答案是從這里獲取的 -使用上述解決方案,您還可以捕獲狀態欄。
PS:檢查代碼的正確方向和屏幕截圖的大小...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.