How to take a screenshot of the current game using CCRenderTexture::saveBuffer method

RongHong Huang wrote:

hello, Jon.
i think there are two points you may check:
firstly, have you render what you want in the texture?
its architecture looks like this:
rend~~>begin;
sprite~~>visit();
rend->end();
>
>
secondly, saveBuffer has two override functions as follows, one is for filepath, and the other is for filename, do you use it correctly?

         /** saves the texture into a file */
    // para szFilePath      the absolute path to save
    // para x,y         the lower left corner coordinates of the buffer to save
    // pare nWidth,nHeight    the size of the buffer to save
    //                        when nWidth = 0 and nHeight = 0, the image size to save equals to buffer texture size
    bool saveBuffer(const char *szFilePath, int x = 0, int y = 0, int nWidth = 0, int nHeight = 0);

    /** saves the texture into a file. put format at the first argument, or ti will be overloaded with
     * saveBuffer(const char *szFilePath, int x = 0, int y = 0, int nWidth = 0, int nHeight = 0) */
    // para name        the file name to save
    // para format      the image format to save, here it supports kCCImageFormatPNG and kCCImageFormatJPG */
    // para x,y         the lower left corner coordinates of the buffer to save
    // pare nWidth,nHeight    the size of the buffer to save
    //                        when nWidth = 0 and nHeight = 0, the image size to save equals to buffer texture size
    bool saveBuffer(int format, const char *name, int x = 0, int y = 0, int nWidth = 0, int nHeight = 0);

I was using the second function with the filePath instead of the name so i guess that the problem could be that. Thanks

ok, on android it works.

but, how can i save it as a image on iphone to show it in the device galery?
it saves the images to app documents and there is no access from device menu.

any ideas?

Super edit:

I read phone instead of iPhone :S

If you want to save it to the iPhone, the best way is to create a objective-c proxy which calls to UIImageWriteToSavedPhotosAlbum(yourUIImage, nil, nil, nil);

thanks, but on android it already works!

my question is only for iPhone!

UPDATE:
ok, i will try it!

another question :slight_smile:

how can i easily convert a CCRenderTexture to a UIImage?

answer:

load UIImage from documents path:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                                                         NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithUTF8String: filename] ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];

    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

Hello,
I read whole the discussion, but didnt find solution how to move saved image to Android image galery.

So can you please show me example of moving image to galery (not only to sdcard)?

Thanks

Hello,
after long time. Here is quick solution for add image to android gallery.

mPackageName = getApplication().getPackageName();

Bitmap imageBitmap = BitmapFactory.decodeFile("/data/data/" + mPackageName + "/" + imageName);
Uri capturedImage = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(mInstance.getContentResolver(), imageBitmap, "Title", "Description"));
1 Like

Hi, This worked for me in android. PNG file is saved at data/data/com.yourpackage/DB_Screenshot.png.

CCSize winSize = CCDirector::sharedDirector~~>getWinSize;
CCRenderTexture * tex = CCRenderTexture::create;
tex~~>setPosition);
tex~~>begin;
this~~>getParent~~>visit;
tex~~>end;
//save to jpg
const char
file;
file = ā€œDB_Screenshot.pngā€;
bool status;
status = tex->saveToFile(file, kCCImageFormatPNG);
CCLog(ā€œStatus :: %dā€,status);*

That works on devices like ipad1 or iphone3 and 4.
I have tested it on ipad3 and iphone 5 and it doesnā€™t workā€¦ I think itā€™s because of the resolution. When we have textures bigger than 1024x1024px it doesnā€™t generate the image.
Is there any way to solve it? Maybe resizing the CCRenderTexture?

Thanks!

Hi, Iā€™m a beginner of cocos2dx, I read your post about add image to android gallery

mPackageName = getApplication().getPackageName();

Bitmap imageBitmap = BitmapFactory.decodeFile("/data/data/" + mPackageName + ā€œ/ā€ + imageName);
Uri capturedImage = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(mInstance.getContentResolver(), imageBitmap, ā€œTitleā€, ā€œDescriptionā€));

may I ask how you define ā€œmInstanceā€? sorry for my stupid question

Hi,
this is easy. Its stored Application instance.

Example:

	public class Application extends Cocos2dxActivity
{
	private static Application mInstance = null;
	
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		mInstance = this;
		
		//...
	}
}

tex->saveToFile(file, kCCImageFormatPNG);

helpful Conversation