Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

captureScreen in cocos-2d C++ - how to use or save the captured image?

Tags:

c++

cocos2d-x

I want to have a button in my (iOS) app that takes a screenshot of the current screen and then attaches it to a text message.

Like I have seen in this other app...

enter image description here

I have message sending working, and I think I have screenshot working, but I don't know where the screenshot is saved or how to use it.

My message sending is called from a button in the app...

void GameOverScene::messageCallBack(cocos2d::Ref *sender) {
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect(ALL_BUTTONS_CLICK_SOUND_NAME);
utils::captureScreen( CC_CALLBACK_2(GameOverScene::afterCaptured, this), "screenshot.png" );
__String *messageTextOne = __String::create("sms:&body=Hey,%20I%20just%20hit%20a%20score%20of%20");
__String *messageTextTwo = __String::createWithFormat("%i", score);
__String *messageURL = __String::createWithFormat("%s%s", messageTextOne->getCString(), messageTextTwo->getCString());
Application::getInstance()->openURL(messageURL->getCString());
}

and the screenshot function is...

void GameOverScene::afterCaptured( bool succeed, const std::string &outputFile ) {
if (succeed) {
    log("Screen capture succeeded");
    Size screenSize = Director::getInstance()->getWinSize();
    RenderTexture * tex = RenderTexture::create(screenSize.width, screenSize.height);
    tex->setPosition(screenSize.width/2, screenSize.height/2);
    tex->begin();
    this->getParent()->visit();
    tex->end();
    tex->saveToFile("Image_Save.png", Image::Format::PNG);
} else {
    log("Screen capture failed");
}
}

I get the message in the console "Screen capture succeeded", and my message app opens up with the prefilled text message.

What I need to do is to add the screenshot to this message, but I cant see how to do that, or where the screenshot is saved, or how to use the saved screenshot.

like image 924
R2D2 Avatar asked May 18 '17 19:05

R2D2


2 Answers

If it succeeded it's saved in private application directory. Use software like iExplorer, find your app and it should be inside Documents directory. If you want to see it in Photos you have manually add it there.

In order to achieve this, you have to call UIImageWriteToSavedPhotosAlbum

see: How to save picture to iPhone photo library?

You have to create a function in AppController.m and use it from here. You can call objc code from c++. It requires UIImage so first you have to pass filepath and load uiimage from it and then finally add to gallery.

like image 72
Makalele Avatar answered Oct 15 '22 17:10

Makalele


You can use something like this to get the path of image saved:

void  CustomLayerColor::saveFile(Node*node, std::string fileName, const renderTextureCallback& callBack) {
    float renderTextureWidth = node->getBoundingBox().size.width;
    float renderTextureHeight = node->getBoundingBox().size.height;        

    RenderTexture * renderTexture = RenderTexture::create(renderTextureWidth,renderTextureHeight);
    renderTexture->begin();

    node->visit();

    renderTexture->end();
    renderTexture->saveToFile(fileName, Image::Format::PNG, true,callBack);
}

void CustomLayerColor::onSaveFileCallBack(RenderTexture * mRenderTexture, const std::string& savedFileNameWithFullPath) {

}
like image 32
Yogesh Kumar Avatar answered Oct 15 '22 17:10

Yogesh Kumar