我想转换CCSpriteBatchNode
为 a UIImage
,有没有可能的方法呢?我知道可以转换CCSprite
为UIImage
,但是当我尝试使用 时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];
}