我一直在阅读关于如何以编程方式截取屏幕截图的解决问题,但我似乎无法在sprite kit中获得我所读到的内容。例如:
此问题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);我给了我这个信息,我在我的游戏上测试了它,但它不能工作,因为window在SKScene上无法识别。我试着用scene来代替它,但这不起作用。有什么建议吗?
我还尝试了这个:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();我在这个问题中找到了:ios Sprite Kit screengrab?
但它似乎也不起作用,因为它不识别规模,或第二行的界限。
发布于 2014-11-05 06:49:04
这是在-Kit中截屏的最佳解决方案
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;发布于 2015-08-04 09:34:25
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发布于 2015-07-31 15:29:22
// 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
}https://stackoverflow.com/questions/23790892
复制相似问题