5

I created CCSprite and add it to the main view. Then I want to see the sprite image, so I saved it to PNG But lots of them doesn't saved correctly. Only show white background. I can't know the reason, and also found some articles via Google, but they didn't help me at all. How can I solve this?

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];
James Webster
  • 31,873
  • 11
  • 70
  • 114
bTagTiger
  • 1,261
  • 5
  • 23
  • 38
  • 1
    I'm not sure on the purpose of your code. Why are you trying to save the sprite to file when you are reading it from a file in the first place? – James Webster Sep 12 '11 at 11:24
  • I also need an answer to this question. I applies opengl/shader effects to a ccsprite and now need to save it as a PNG. – Alex L Oct 26 '11 at 16:17
  • 1
    [sprite setPosition:ccp(size.width/2.0f, size.height/2.0f)]; If you don't put the sprite inside the CCRenderTexture, you won't be able to see it. – Jay Apr 02 '12 at 21:07
  • 1
    This question ought to be re-opened, it's still relevant. Jay's answer is correct, FWIW. –  Jul 16 '12 at 01:56
  • Why this question should be closed??? Anyway, use CCRenderTexture and its method saveToFile(). – Jinbom Heo Dec 02 '13 at 06:23

0 Answers0