从 SKShapeNode 创建 SKTexture [英] Create an SKTexture from an SKShapeNode

查看:17
本文介绍了从 SKShapeNode 创建 SKTexture的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法从 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).

推荐答案

你可以使用SKView方法调用textureFromNode:

这是来自参考:

textureFromNode:

渲染并返回包含节点内容的 Sprite Kit 纹理.

Renders and returns a Sprite Kit texture that contains the node’s contents.

- (SKTexture *)textureFromNode:(SKNode *)node

参数

节点

一个节点对象,表示您要渲染到纹理的内容.

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]).

更新——在探索 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 :)

是的...我刚刚创建了一个 SKShapeNodes 池,我可以根据需要重复使用它.这有什么不同 :) 所以只需在需要时重新定义路径,完成后使用 return to your pool,它会在那里等着你稍后再玩.

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

使用完形状节点后,只需将其返回到池中并将路径设置为 .例如:

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;

我知道这是一种解决方法,而不是理想的解决方案,但对于任何想要使用大量 SKShapeNode 且不会耗尽内存的人来说,这无疑是一个非常可行的解决方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆