[solved] Saving screenshoot on iOS with Cocos2d-x v2.6

Dear All,

I am Andi Taru from Educa Studio

I am using cocos2d-x v2.6 (C++, XCode)
I want to save CCRenderTexture into PNG files (in this case on iOS 7 - iPhone 5)
Saving success, but I am searching on Gallery no Image found.

This is my code:

CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);
texture->setPosition(ccp(size.width/2, size.height/2));
texture->begin();
CCDirector::sharedDirector()->getRunningScene()->visit();
texture->end();

std::string time = currentDateTime();
std::string path = CCFileUtils::sharedFileUtils()->getWritablePath() + time + “.png”;
return texture->saveToFile(path.c_str());

Anyone can help me?
How to save CCRenderTexture into spesific filename on iOS Gallery Image.

Thank you before

The code you have will save the image inside your app.

To save into iOS Camera Roll, you will need to use iOS specific functions. You should probably use this one: https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIKitFunctionReference/index.html#//apple_ref/c/func/UIImageWriteToSavedPhotosAlbum which takes an UIImage. I do not know how to convert a CCRenderTexture to UIImage, but since you save it to a png, you should be able to load UIImage from the png file.

Also, I don’t think you’ll be able to use a specific filename. iOS don’t really work like that: iOS tend to hide as much as possible the filesystem from the user, so the camera roll doesn’t keep images filename.

Hi Fradow.

All works perfectly!
I am just adding this methods on my Objective-C class:

// filename
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];

NSLog(@"copy from: %@",screenshotPath);

//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);

Thanks!