我一直在阅读关于如何以编程方式截屏的已解决问题,但似乎无法在Sprite Kit中实现所读内容。例如:
我在这个问题中找到了一个答案:iOS 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截屏? 但它似乎也没有起作用,因为第二行代码中没有识别到比例或边界。