当我试图在 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/