2

When I am trying to grab a screen shot in spritekit. I've tried all the methods I can find online. They are not working.

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, self.view.opaque, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *gameOverScreenImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

just not working

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

cant recognize self.bounds and scale.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • So your problem isn't that the screenshot isn't taken, but that `bounds` and `scale` are not recognized? – JustSid Jul 28 '14 at 19:54
  • @JustSid You can say that. The second paragraph is claimed to be successfully working in sprite kit. But it doesn't get recognized when I try to use it. There is no warning about the first paragraph of code, it just not working. – user3882307 Jul 28 '14 at 20:15
  • i use your second approach in one of my games and works correctly, you should call that method from your "ViewController.h" not from "MyScene.h" – Julio Montoya Jul 28 '14 at 21:14
  • `just not working` explain further please. – Andrew Jul 28 '14 at 21:20

1 Answers1

3

I’m not sure whether -drawViewHierarchyInRect:afterScreenUpdates: is supposed to work with SKView (I assume so), but the reason that second example doesn’t compile is that (1) you’re trying to call it and bounds on self (presumably a UIViewController) rather than self.view and (2) you haven’t defined the value of scale. Just change those two lines:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, [UIScreen mainScreen].scale);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

Also, the second parameter to UIGraphicsBeginImageContextWithOptions—“opaque”—shouldn’t be NO unless your view is transparent, and setting “afterScreenUpdates” to YES is only necessary in some circumstances which probably don’t include this one. You may see better performance by changing those.

Noah Witherspoon
  • 57,021
  • 16
  • 130
  • 131
  • It works! I tried change self to self.view, but i did not changed the first one. Thanks for helping me solve this problem and your explanation make a lot of sense to me. – user3882307 Jul 28 '14 at 22:20
  • I am sorry that I cant vote up cuz I just registered. Will vote it up once I got 15 reputation. – user3882307 Jul 28 '14 at 22:22
  • No problem. You can click the checkbox next to an answer to accept it as having resolved your question. – Noah Witherspoon Jul 28 '14 at 22:50