我想将CCSpriteBatchNode
转换为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];
}
答案 0 :(得分:0)
我自己需要这个。这是ARC代码,并设置为CCLabelBMFont的类别。
@interface CCLabelBMFont (UIImage)
- (UIImage *)image;
@end
@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