2014-05-21 71 views
3

我一直在閱讀已解決的問題,如何以編程方式截圖,但我似乎無法得到我讀過的工作在精靈套件。例如:如何以編程方式在Sprite-Kit中截取屏幕截圖?

這個問題​​3210

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

UPDATE 2011年4月:視網膜顯示,第一行變成這樣:

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

給了我這個信息,這是我在我的遊戲測試但它不起作用,因爲SKScene上沒有識別窗口。我試圖用場景替換它,但沒有奏效。有什麼建議麼?

我也試過這樣:

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

這是我在這個問題中發現:ios Sprite Kit screengrab?

但它似乎沒有工作或者是因爲它不承認的規模,或者邊界在第二線。

+0

還不如在空白圖像或只有部分工作的最佳解決方案?執行屏幕截圖時,屏幕上有什麼內容? – sangony

+0

它是菜單上的遊戲,它顯示用戶的高分,並且我想在用戶點擊Twitter或Facebook按鈕時執行它,然後將其附加到共享帖子。因此,如果可能的話,我希望它可以保存到應用程序中,以便可以在「share.png」的社交分享中引用它,如果這樣做合理的話。 @sangony – user3576196

+0

它沒有工作的方式是因爲它不承認第一行中的縮放比例,並且在第二行中沒有修改self.bounds @sangony – user3576196

回答

-1

您可以拍攝任何視圖的屏幕截圖。 UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *gameOverScreenImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

+0

我只是把它放在按鈕方法裏面嗎?圖像保存在哪裏? – user3576196

+0

是的,你可以把它放在按鈕方法中。 'gameOverScreenImage'具有該圖像。你可以像你一樣使用它。 –

6

這是拿一個屏幕拍在你的雪碧套件

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1); 
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; 
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
return viewImage; 
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 
} 
1
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 
相關問題