Cocos2d How to save sprite to image

/* Save image from sprite */
+ (void) saveSpriteToImage:(CCSprite*) sprite {
   
    CGSize size = [sprite contentSize];
   
    int nWidth = size.width;
    int nHeight = size.height;

    CCRenderTexture* render = [CCRenderTexture renderTextureWithWidth:nWidth height:nHeight];
    [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",1]];
    [render saveBuffer:pngPath format:kCCImageFormatPNG];
    [[CCSpriteFrameCache sharedSpriteFrameCache] removeUnusedSpriteFrames];
    [[CCTextureCache sharedTextureCache] removeUnusedTextures];
}

Comments

Popular posts from this blog

LINUX Move and copy files using SSH

PHP Predefined Variables

Java : Variables Declaring