SpriteKit SKTexture 崩溃 [英] SpriteKit SKTexture Crash
问题描述
不知道为什么这不起作用.
Not sure why this doesnt work.
当它尝试创建 ship 节点时,它会因 EXC_BAD_ACCESS 而崩溃.
It crashes with EXC_BAD_ACCESS when it tries to create the ship node.
SKTexture *tex = [SKTexture textureWithImageNamed:@"Spaceship"];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:@0.6 forKey:kCIInputIntensityKey];
SKTexture *texDone;
if (filter) {
texDone = [tex textureByApplyingCIFilter:filter];
}
if (texDone) {
SKSpriteNode *ship = [SKSpriteNode spriteNodeWithTexture:texDone];
[self addChild:ship];
ship.position = CGPointMake(200, 200);
}
与创建飞船时发生相同的崩溃.
same crash as creating the ship.
我已经使用了这个 SKEffect,但它的代码要多得多?对于相同的过滤器.以下工作.
I have used this SKEffect, but it is a lot more code ? For same filter. The following works.
SKSpriteNode *spriteToFilter = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone"];
[filter setValue:@1.0 forKey:kCIInputIntensityKey];
SKEffectNode *effectNode = [SKEffectNode node];
effectNode.filter = filter;
effectNode.shouldEnableEffects = YES;
[effectNode addChild:spriteToFilter];
[self addChild:effectNode];
effectNode.position = CGPointMake(200, 200);
推荐答案
看起来是 SKTexture
中的错误,如开发论坛 这里.在 lldb 跟踪之后,我猜这是一个内存管理问题,它在使用过滤器时如何分配图像数据缓冲区.如果你真的想避免使用 SKEffectNode
,这段代码应该可以通过直接处理过滤器并避免对 SKTexture
的依赖来解决它.
Looks to be a bug in SKTexture
as discussed in the dev forums here. Following the lldb trace, I would guess it's a memory management problem with how it allocates the image data buffers when using filters. If you really want to avoid using SKEffectNode
, this code should get you around it by handling the filters directly and avoiding the reliance on SKTexture
.
UIImage *ship = [UIImage imageNamed:@"Spaceship.png"];
CIImage *shipImage = [[CIImage alloc] initWithImage:ship];
CIFilter *filter = [CIFilter filterWithName:@"CISepiaTone" keysAndValues:kCIInputImageKey, shipImage, @"inputIntensity", [NSNumber numberWithFloat:0.6], nil];
CIContext *context = [CIContext contextWithOptions:nil];
CIImage *out = [filter outputImage];
CGImageRef cg = [context createCGImage:out fromRect:[out extent]];
SKTexture *texDone = [SKTexture textureWithCGImage:cg];
在这里测试和工作,但使用您发布的 SKEffectNode
代码可能更容易.不确定哪个更有效,你必须和他们一起玩.
Tested and working here, but probably easier to just use the SKEffectNode
code you posted. Not sure which is more efficient, you'll have to play around with them.
// adding the texture to a sprite with above filter works
SKSpriteNode *spriteToFilter = [SKSpriteNode spriteNodeWithTexture:texDone];
[self addChild:spriteToFilter];
spriteToFilter.position = CGPointMake(200, 200);
这篇关于SpriteKit SKTexture 崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!