4

I've been reading resolved questions on how to programmatically take a screenshot but I can't seem to get what I've read to work in sprite kit. For instance:

This question 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];

UPDATE April 2011: for retina display, change the first line into this:

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

gave me this information, which I tested on my game but it didn't work because window was not recognized on an SKScene. I tried replacing it with scene but that didn't work. Any suggestions?

I also tried this:

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

Which I found in this question: ios Sprite Kit screengrab?

But it didn't seem to work either because it didn't recognize scale, or bounds in the second line.

Community
  • 1
  • 1
user3576196
  • 275
  • 4
  • 11
  • Not work as in blank image or only partial? What content do you have on your screen when the screenshot is executed? – sangony May 21 '14 at 18:45
  • It's the game over menu, which displays the users highscore, and I want to execute it when the user taps the twitter or facebook button, then attach it to the share post. So I want it to save to the app if possible, so that it can be referenced in the social sharing with "share.png" if that makes sense. @sangony – user3576196 May 21 '14 at 18:55
  • The way it didn't work was because it didn't recognize scale in the first line, and in the second line it didn't recgonize self.bounds @sangony – user3576196 May 21 '14 at 18:56
  • That is exactly what I do in my app!! Check it out https://itunes.apple.com/ca/app/save-square-squares-adventures/id967632086?mt=8 ! I didn't want the user to cheat so i take a screenshot as a proof!! – Aryaman Goel Aug 04 '15 at 01:36

4 Answers4

6

This is the best solution to take a screen shoot in your Sprite-Kit

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 1);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return  viewImage;
Fabio
  • 1,913
  • 5
  • 29
  • 53
  • [self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; - Is causing game to slow down is there any way to slove this issue – Albi Mar 30 '19 at 17:43
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
Aryaman Goel
  • 538
  • 1
  • 4
  • 15
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 
}
Raksha Saini
  • 604
  • 12
  • 28
-2

you can take screen shot for a any view.UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0); [self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *gameOverScreenImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

Sandeep Singh
  • 268
  • 1
  • 9