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

2

6

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.

Iconic answered 18/5, 2017 at 19:59 Comment(0)
J
1

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.

Jacinto answered 19/5, 2017 at 12:23 Comment(2)
What I ultimately want to do with this screenshot is send it to the iPhone message app, so it can be sent out as a text. How would I go about sending the screenshot to messages?Iconic
Try this: #21700256Jacinto
S
1

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) {

}
Scurlock answered 10/8, 2017 at 5:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.