我一直在阅读有关如何以编程方式截取屏幕截图的已解决问题,但我似乎无法将我所阅读的内容用于 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/