当我试图在 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();

无法认识自我界限和尺度。

有帮助吗?

解决方案

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

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

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top