从SKShapeNode创建SKTexture [英] Create an SKTexture from an SKShapeNode
问题描述
有没有办法从SKShapeNode创建SKTexture?已知形状节点会导致内存问题,我正在寻找一种从它们创建静态图形的方法(最好是.png)。
Is there a way to create an SKTexture from an SKShapeNode? Shape nodes are known to cause memory issues and I am looking for a way to create a static graphic from them (.png preferably).
推荐答案
您可以使用名为 textureFromNode的
SKView
方法:
You can use the SKView
method called textureFromNode:
以下是参考资料:
textureFromNode:
呈现并返回包含节点内容的Sprite Kit纹理。
Renders and returns a Sprite Kit texture that contains the node’s contents.
- (SKTexture *)textureFromNode:(SKNode *)node
参数
Parameters
节点
表示要渲染到纹理的内容的节点对象。
A node object representing the content you want to render to the texture.
返回值
保存渲染图像的Sprite Kit纹理。
A Sprite Kit texture that holds the rendered image.
讨论
正在渲染的节点不需要出现在视图的呈现场景中。创建的新纹理的大小等于节点的calculateAccumulatedFrame方法返回的矩形。如果节点不是场景节点,则使用清晰的背景颜色([SKColor clearColor])进行渲染。
Discussion The node being rendered does not need to appear in the view’s presented scene. The new texture is created with a size equal to the rectangle returned by the node’s calculateAccumulatedFrame method. If the node is not a scene node, it is rendered with a clear background color ([SKColor clearColor]).
UPDATE--稍微探索SKShapeNode并验证确实是一个内存泄漏问题围绕分配路径然后删除节点,我有一个想法...
UPDATE-- After exploring SKShapeNode a bit and verifying there indeed was a memory leak issue revolving around assigning a path and then removing the node, I had an idea...
不是一个新想法,更多是一个改变用途的想法。这个奇妙的想法实际上允许我使用SKShapeNodes到我心中的内容:)
Not really a new idea, more of a repurposed one. This wonderful idea actually allowed me to use SKShapeNodes to my hearts content :)
POOLING
是的......我刚创建了一个SKShapeNodes池,我根据需要重用了它。这有什么不同:)所以只需要在需要的时候重新定义路径,当使用返回你的游泳池完成时,它将等待你以后再玩一次。
Yep... I just created a pool of SKShapeNodes that I reused as needed. What a difference that makes :) So simply redefine the path whenever needed, when done using return to your pool, and it'll be waiting there for you to play with again at a later time.
您只需在需要时重新定义路径,使用返回游泳池完成后,它将等待您以后再次玩游戏。
You simply redefine the path whenever needed, when done using return to your pool, and it'll be waiting there for you to play with again at a later time.
在您的 SKScene
中创建一个ivar或属性 NSMutableArray
池并在初始化 SKScene
时创建它。您可以在初始化期间使用形状节点填充数组,也可以根据需要创建它们。
Create a ivar or property NSMutableArray
in your SKScene
called pool and create it when you init the SKScene
. You can either populate the array with your shape nodes during init, or you can create them as needed.
这是我创建的用于从中获取新节点的快速方法游泳池:
This is something quick method I created for grabbing a new node from the pool :
-(SKShapeNode *)getShapeNode
{
if (pool.count > 0)
{
SKShapeNode *shape = pool[0];
[pool removeObjectAtIndex:0];
return shape;
}
// if there is not any nodes left in the pool, create a new one to return
SKShapeNode *shape = [SKShapeNode node];
return shape;
}
因此,无论你在场景中需要SKShapeNode,你都可以这样做:
So wherever in the scene you need a SKShapeNode you'd do this :
SKShapeNode *shape = [self getShapeNode];
// do whatever you need to do with the instance
当你完成使用shape节点,只需将其返回到池并将路径设置为。例如:
When you are done using the shape node, just return it to the pool and set the path to . For example :
[pool addObject:shape];
[shape removeFromParent];
shape.path = NULL;
我知道这是一种解决方法而不是理想的解决方案,但对于任何人来说,这肯定是一种非常可行的解决方法想要使用大量的SKShapeNodes而不是出血记忆。
I know it's a workaround and not an ideal solution, but certainly this is a very viable workaround for anyone wanting to use a large number of SKShapeNodes and not bleed memory.
这篇关于从SKShapeNode创建SKTexture的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!