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

use*_*196 4 screenshot uiimage ios sprite-kit

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

这个问题如何以编程方式截取屏幕截图

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

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

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

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

我也试过这个:

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

我在这个问题中找到了:ios Sprite Kit screengrab?

但它似乎没有用,因为它不能识别第二行中的比例或界限.

Fab*_*bio 6

这是在Sprite-Kit中进行屏幕拍摄的最佳解决方案

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