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.

R2D2
  • 2,620
  • 4
  • 24
  • 46

2 Answers2

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.

Community
  • 1
  • 1
Makalele
  • 7,431
  • 5
  • 54
  • 81
  • 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? – R2D2 May 19 '17 at 15:27
  • Try this: http://stackoverflow.com/questions/21699756/send-picture-text-in-imessage-in-ios-programatically – Makalele May 19 '17 at 17:15
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) {

}
Anh Pham
  • 2,108
  • 9
  • 18
  • 29