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

4
我一直在阅读关于如何以编程方式截屏的已解决问题,但似乎无法在Sprite Kit中实现所读内容。例如:

这个问题如何以编程方式进行截屏

   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月更新:针对Retina显示屏,请将第一行更改为:

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

我收到了这个信息,我在我的游戏中测试了一下,但是它没有起作用,因为window在SKScene上无法识别。我尝试将其替换为scene,但也没有起作用。有什么建议吗?

我还尝试了这个:

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

我在这个问题中找到了一个答案:iOS Sprite Kit截屏? 但它似乎也没有起作用,因为第二行代码中没有识别到比例或边界。

执行截图时,您的屏幕显示什么内容?是完全空白的图像还是部分显示? - sangony
这是游戏结束菜单,显示用户的最高分,我想在用户点击 Twitter 或 Facebook 按钮时执行它,然后将其附加到分享帖子。所以如果可能的话,我希望它保存到应用程序中,这样就可以在社交分享中使用 "share.png" 作为参考。@sangony - user3576196
它无法工作的原因是它在第一行中没有识别比例尺,在第二行中没有识别self.bounds @sangony。 - user3576196
这正是我在我的应用程序中所做的事情!!请查看 https://itunes.apple.com/ca/app/save-square-squares-adventures/id967632086?mt=8!我不想让用户作弊,所以我会截屏作为证明!! - Aryaman Goel
4个回答

6

这是在Sprite-Kit中截屏的最佳解决方案

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

[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; - 会导致游戏变慢,有没有解决这个问题的方法? - Albi

3
func screenShot() {

    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0)
    self.view!.drawViewHierarchyInRect(self.view!.bounds, afterScreenUpdates: true)
    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    var Image = image //The image is stored in the variable Image

}

screenshot() //Calls the function and takes a screenshot

1
// Full Screen Shot function. Hope this will work well in spritekit.   
func screenShot() -> UIImage {                                                    
 UIGraphicsBeginImageContext(CGSizeMake(frame.size.width, frame.size.height))
 var context:CGContextRef  = UIGraphicsGetCurrentContext()
 self.view?.drawViewHierarchyInRect(frame, afterScreenUpdates: true)
 var screenShot = UIGraphicsGetImageFromCurrentImageContext()
 UIGraphicsEndImageContext();  
 return screenShot 
}

-2

您可以为任何视图截屏。UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *gameOverScreenImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();


我只需要把它放在按钮方法里吗?图片保存在哪里? - user3576196
是的,你可以将它放在按钮方法里面。'gameOverScreenImage' 就是那张图片。你可以随意使用它。 - Sandeep Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接