Jason McMaster
Jason McMaster

Reputation: 103

Saving a screenshot in CoCos2d V2.xx using CCRenderTexture

I know there are quite a few examples of how to save a screen in CoCos2d using CCRenderTexture, but they just don't seem to work for me. I've written a coloring book application for a client and they, of course, want to be able to save out images. I've tried a ton of different ways and bastardized a bunch of examples to no avail. Lately, I've been receiving this error:

2012-03-24 13:07:03.749 Coloring Book[823:1be03] cocos2d: ERROR: Failed to save file:/Users/macbookpro/Library/Application Support/iPhone Simulator/5.1/Applications/76F88977-AD3A-47B8-8026-C9324BB3636E/Documents/Users/macbookpro/Library/Application Support/iPhone Simulator/5.1/Applications/76F88977-AD3A-47B8-8026-C9324BB3636E/Documents/testimagename.png to disk

I get something similar when running from the device. Here's my screenshot code:

- (void) takeScreenShot
  {
     NSString* file = @"testimagename.png";

NSArray* paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* screenshotPath = [documentsDirectory 
                            stringByAppendingPathComponent:file];

[CCDirector sharedDirector].nextDeltaTimeZero = YES;

CGSize winSize = [CCDirector sharedDirector].winSize;
CCRenderTexture* rtx = 
[CCRenderTexture renderTextureWithWidth:winSize.width 
                                 height:winSize.height];
[rtx begin];
[Page visit];
[rtx end];

// save as file as PNG
[rtx  saveToFile:screenshotPath
         format:kCCImageFormatPNG];
 }

It's probably something simple, but it has been driving me nuts for a few days! Please, Stack Overflow, make me feel stupid and fix my problem!

Upvotes: 3

Views: 3213

Answers (1)

Jason McMaster
Jason McMaster

Reputation: 103

The problem I was having comes down to defining a path. You don't need to define a path to the Documents section of the device, Cocos2D saves it to the Documents by default. I put this together (note thanks very much to LearnCocos2D for some of the code I'm using) to save the layers I want and then save the screen to the Photo Library.

- (void) takeScreenShot
{

//name the file we want to save in documents
NSString* file = @"//imageforphotolib.png";

//get the path to the Documents directory
NSArray* paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* screenshotPath = [documentsDirectory 
                            stringByAppendingPathComponent:file];

[CCDirector sharedDirector].nextDeltaTimeZero = YES;

//creating standard screensize variable
CGSize winSize = [CCDirector sharedDirector].winSize;

//we're using transparancies as the images, 
//so we load this white page to give a backdrop
CCSprite *whitePage = [CCSprite spriteWithFile:@"whitePage.png"];
whitePage.position = ccp(winSize.width/2, winSize.height/2);

//create a render texture to hold our images
CCRenderTexture* rtx = 
[CCRenderTexture renderTextureWithWidth:winSize.width 
                                 height:winSize.height];
[rtx begin];// open the texture
[whitePage visit];//add a white page to the background
[Page visit];//put in the background image
[target visit];//put in the coloring layer
[rtx end];//close the texture

// save as file as PNG
[rtx  saveToFile:@"imageforphotolib.png"
         format:kCCImageFormatPNG];

//get the screenshot as raw data
NSData *data = [NSData dataWithContentsOfFile:screenshotPath];
//create an image from the raw data
UIImage *img = [UIImage imageWithData:data];
//save the image to the users Photo Library
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
 }

Upvotes: 6

Related Questions