2

我想转换CCSpriteBatchNode为 a UIImage,有没有可能的方法呢?我知道可以转换CCSpriteUIImage,但是当我尝试使用 时CCSpriteBatchNode,它会崩溃:

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'CCSpriteBatchNode should NOT be root node'

谢谢!

这是将 CCSprite 转换为 UIImage 的代码。

+ (UIImage *)renderUIImageFromSprite:(CCSprite *)sprite {
    CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width; height:sprite.contentSize.height;];
    [renderer begin];
    [sprite visit];
    [renderer end];
    return [renderer getUIImageFromBuffer];
}

编辑:这是工作代码。

+ (UIImage *)renderUIImageFromCCLabelBMFont:(CCLabelBMFont *)bmfont {
    GLProgram *program = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor];
    glUseProgram(program->program_);

    int textureWidth = bmfont.contentSize.width;
    int textureHeight = bmfont.contentSize.height;

    CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:textureWidth height:textureHeight];
    bmfont.anchorPoint = ccp(0.0, 0.0);

    CCNode *node = [CCNode node];
    [node addChild:bmfont];

    [renderer begin];
    [node visit];
    [renderer end];

    return [renderer getUIImageFromBuffer];
}
4

1 回答 1

0

我自己需要这个。这是 ARC 代码并设置为 CCLabelBMFont 的类别。

CCLabelBMFont+UIImage.h

@interface CCLabelBMFont (UIImage)

- (UIImage *)image;

@end

CCLabelBMFont+UIImage.m

@implementation CCLabelBMFont (UIImage)

- (UIImage *)image
{
    CCGLProgram *program = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTextureColor];
    glUseProgram(program->program_);

    CCRenderTexture *renderer = [[CCRenderTexture alloc] initWithWidth:(int)self.contentSize.width
                                                                height:(int)self.contentSize.height
                                                           pixelFormat:kCCTexture2DPixelFormat_RGBA8888];

    CCLabelBMFont *labelBMFont = [[CCLabelBMFont alloc] initWithString:string_
                                                               fntFile:fntFile_
                                                                 width:width_
                                                             alignment:alignment_
                                                           imageOffset:imageOffset_];

    labelBMFont.anchorPoint = ccp(0.0, 0.0);

    CCNode *node = [CCNode node];
    [node addChild:labelBMFont];

    [renderer begin];
    [node visit];
    [renderer end];

    return [renderer getUIImage];
}

@end
于 2012-07-16T13:23:48.167 回答