首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Sprite-Kit中编程截图?

如何在Sprite-Kit中编程截图?
EN

Stack Overflow用户
提问于 2014-05-22 02:17:11
回答 4查看 2.7K关注 0票数 4

我一直在阅读关于如何以编程方式截取屏幕截图的解决问题,但我似乎无法在sprite kit中获得我所读到的内容。例如:

此问题How to take a screenshot programmatically

代码语言:javascript
复制
   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月:对于视网膜显示,将第一行更改为:

代码语言:javascript
复制
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
    UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
else
    UIGraphicsBeginImageContext(self.window.bounds.size);

我给了我这个信息,我在我的游戏上测试了它,但它不能工作,因为window在SKScene上无法识别。我试着用scene来代替它,但这不起作用。有什么建议吗?

我还尝试了这个:

代码语言:javascript
复制
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

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

但它似乎也不起作用,因为它不识别规模,或第二行的界限。

EN

回答 4

Stack Overflow用户

发布于 2014-11-05 06:49:04

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

代码语言:javascript
复制
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return  viewImage;
票数 6
EN

Stack Overflow用户

发布于 2015-08-04 09:34:25

代码语言:javascript
复制
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
票数 3
EN

Stack Overflow用户

发布于 2015-07-31 15:29:22

代码语言:javascript
复制
// 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 
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23790892

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档