Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to get a screen grab of a view that has a SKScene in it. The technique I am using is:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

This works great with normal UIViews, but for whatever reason it is ignoring all the sprites in the SKScene.

I'm not sure if this is a bug, or if Sprite Kit's rendering is separate from UIGraphics.

Question: How do I get a screen grab of an SKScene when the way that worked for UIViews seems to not work with Sprite Kit, or has anyone had success using UIGraphics context with Sprite Kit?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
374 views
Welcome To Ask or Share your Answers For Others

1 Answer

You almost have it, but the problem is as explained in the comment above. If you want to capture SKScene contents, try something like this instead:

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

The solution is to basically use the new method drawViewHierarchyInRect:afterScreenUpdates instead, which is the best we have for now; note, it's not exactly speedy, so doing this in realtime will not be pretty.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...