13

SpriteKit をいじって、SKNode の「グラブ」を UIImage にキャプチャする方法を見つけようとしています。

UIView (または UIView サブクラス) を使用してlayer、ビューのプロパティを使用してグラフィックス コンテキストにレンダリングしました。

例えば。

#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 は UIView のサブクラスではないため、レイヤーによってサポートされているようには見えません。

特定の SKNode を UIImage にレンダリングする方法についてのアイデアはありますか?

4

2 に答える 2

17

これにより、シーン全体がキャプチャされます。

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

特定のノード ブランチだけが必要な場合は、スクリーンショットを撮る前に、キャプチャしたくないすべてのノードを非表示にすることができます。座標に変換された蓄積フレームを使用しUIKitて、問題のノードとその子の領域のみをキャプチャすることもできます。

SKTextureまたは、ノード階層の特定の部分からを取得できます。

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

iOS 9 より前は、 を に変換する方法がありませんでしSKTextureUIImage。ただし、今では簡単です。

UIImage *image = [UIImage imageWithCGImage:tex.CGImage];
于 2013-12-17T09:11:11.733 に答える