如何将CCSprite保存为PNG

时间:2011-09-12 09:07:29

标签: ios4 cocos2d-iphone ccsprite object-code

我创建了CCSprite并将其添加到主视图中。 然后我想看精灵图像,所以我把它保存到PNG 但是很多都没有正确保存。 只显示白色背景。 我不知道原因,也发现了谷歌的一些文章,但他们根本没有帮助我。 我该如何解决这个问题?

CCSprite *sprite = [[CCSprite spriteWithFile:[NSString stringWithUTF8String:name] rect:CGRectMake(startx, starty, w, h)] retain];

float drawX = x, drawY = y; 

CGSize size = [sprite contentSize];

int nWidth = size.width;
int nHeight = size.height;
nWidth *= scale;
nHeight *= scale;
drawX = drawX + nWidth/2;
drawY = drawY - nHeight/2;

ConvertCoordf(&drawX, &drawY);
drawY -= nHeight;
[sprite setScale:scale];
[sprite setPosition:ccp(drawX, drawY)];
[_mainLayer addChild:sprite];

CCRenderTexture* render = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];    
[render begin];
[sprite visit];
[render end];    
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString  *pngPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"new%d.png",i]];
i++;
[render saveBuffer:pngPath format:kCCImageFormatPNG];

[sprite release];

0 个答案:

没有答案