ios - 如何以编程方式在 Spritekit 中截取屏幕截图?

标签 ios uiimage screenshot sprite-kit

我一直在阅读有关如何以编程方式截取屏幕截图的已解决问题,但我似乎无法将我所阅读的内容用于 sprite 工具包。例如:

这个问题How to take a screenshot programmatically

   UIGraphicsBeginImageContext(self.window.bounds.size);
    [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    NSData * data = UIImagePNGRepresentation(image);
    [data writeToFile:@"foo.png" atomically:YES];

2011 年 4 月更新:对于视网膜显示,将第一行更改为:

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

给了我这个信息,我在我的游戏中测试了它,但它没有工作,因为在 SKScene 上无法识别窗口。我尝试用场景替换它,但这没有用。有什么建议么?

我也试过这个:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

我在这个问题中发现:ios Sprite Kit screengrab?

但它似乎也不起作用,因为它无法识别第二行中的比例或边界。

最佳答案

这是在您的 Spritekit 中截取屏幕截图的最佳解决方案

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return  viewImage;

关于ios - 如何以编程方式在 Spritekit 中截取屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23790892/

相关文章:

iphone - 如何防止 UITableViewCell 被移离它自己的部分?

ios - iPhone 4s-UIImage CGImage图像尺寸

ios - 应用内截图并附加到电子邮件 IOS

jenkins - 如何在机器人框架测试报告中添加失败的测试截图?

ios - 我尝试在 iPhone 的后台显示 FCM 通知,但是当我使用 Swift 发送此通知时它不起作用

ios - 在ios8中查找经纬度

objective-c - 从 UIImageViews iOS 创建 png

ios - Xcode 中的图像大小是否需要正好是 1x/2x/3x 比例?

blackberry - 黑莓的截图工具?

ios - ShareKit 插件不工作 - Cordova 1.9 & ShareKit 2.0 & ShareKit js 插件 & Xcode 4.4