Saving Screenshot to CoCos2d V2.xx Using CCRenderTexture
I know there are a few examples of how to save the screen in CoCos2d using CCRenderTexture, but they just don't work for me. I wrote a coloring app for a client and they want to be able to save images of course. I've tried many different ways and bit a bunch of examples to no avail. Lately I have been getting this error:
2012-03-24 13: 07: 03.749 Coloring [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 / Application / 76F88977-AD3A-47B8-8026-C9324BB3636E / Documents / testimagename.png to disk
I am getting something like this when working with 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 turned me on for days! Please stack overflow make me feel stupid and fix my problem!
source to share
The problem I am facing comes down to determining the path. You do not need to define the path to the "Documents" section of the device, Cocos2D saves it to the documents by default. I put this together (note that I am very grateful to LearnCocos2D for some of the code I am 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);
}
source to share