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

Java : Variables Declaring

SQL Self JOIN