SpriteKit SKTexture Crash

℡╲_俬逩灬. 提交于 2019-12-04 06:36:13

问题


Not sure why this doesnt work.

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.

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);

回答1:


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];

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);



回答2:


I had a similar problem with particles in Sprite Kit -- it seemed to be because the source texture was in a Texture Atlas folder, and it didn't have the corresponding @2x image. Try moving the texture out of the texture atlas and/or adding an @2x version of the image.



来源:https://stackoverflow.com/questions/19243111/spritekit-sktexture-crash

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