ios - 如何在 sprite kit 中抓取屏幕截图?

标签 ios objective-c sprite-kit screenshot

当我试图在 spritekit 中抓取屏幕截图时。我已经尝试了所有可以在网上找到的方法。他们不工作。

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *gameOverScreenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

只是不工作

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

无法识别 self.bounds 和 scale。

最佳答案

我不确定 -drawViewHierarchyInRect:afterScreenUpdates: 是否应该与 SKView 一起工作(我假设是这样),但第二个示例无法编译的原因是 (1) 你'重新尝试在 self(大概是 UIViewController)上调用它和 bounds 而不是 self.view 并且 (2) 你还没有定义scale 的值。只需更改这两行:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

另外,UIGraphicsBeginImageContextWithOptions 的第二个参数——“opaque”——不应该是 NO,除非你的 View 是透明的,并且只有在某些可能不需要的情况下才需要将“afterScreenUpdates”设置为 YES不包括这个。通过更改它们,您可能会看到更好的性能。

关于ios - 如何在 sprite kit 中抓取屏幕截图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25003075/

相关文章:

javascript - html5 视频无法在 Apple 机器上的 firefox 中播放

ios - swift 从 Facebook graph Api 请求访问设备数据

ios - 在后台执行 NSTimer

ios - 图标角标(Badge)未出现在 IOS 10 模拟器中

ios - 如何检测 touchBegins 中的所有节点或特定节点

ios - Swift - 以编程方式定位 UIButton 的问题(通用)

ios - MKRouteStep:位置在哪里?

iphone - Apple GameCenter 计分

ios - 如何唯一识别 iPhone/iPad?

swift - 如何对 Sprite 集合运行一个 Action ?