How to render a SKNode to UIImage

懵懂的女人 提交于 2019-12-05 16:44:35

问题


Just playing around with SpriteKit and am trying to figure out how to capture a 'grab' of an SKNode into a UIImage.

With UIView (or a UIView subclass), I have used the layer property of the view to render into a graphics context.

Eg.

#import <QuartzCore/QuartzCore.h>
+ (UIImage *)imageOfView:(UIView *)view {
    UIGraphicsBeginImageContextWithOptions(view.frame.size, YES, 0.0f);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *viewShot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return viewShot;
}

SKNode is not a subclass of UIView and thus does not appear to be backed by a layer.

Any ideas of how I might go about rendering a given SKNode to a UIImage?


回答1:


This will capture the entire scene:

CGRect bounds = self.scene.view.bounds;
UIGraphicsBeginImageContextWithOptions(bounds.size, NO, [UIScreen mainScreen].scale);
[self drawViewHierarchyInRect:bounds afterScreenUpdates:YES];
UIImage* screenshotImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

If you want just a particular node branch you can hide all nodes you don't want to capture before taking the screenshot. You can also use accumulated frame converted to UIKit coordinates to capture only the area of the node and its children in question.

Alternately, you can get an SKTexture from a specific part of the node hierarchy:

SKTexture* tex = [self.scene.view textureFromNode:yourNode];

Prior to iOS 9, there wasn't way to convert an SKTexture back to a UIImage. Now, however, it's trivial:

UIImage *image = [UIImage imageWithCGImage:tex.CGImage];


来源:https://stackoverflow.com/questions/20625378/how-to-render-a-sknode-to-uiimage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!