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);
有帮助吗?

解决方案

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

其他提示

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top