【问题标题】:captureScreen in cocos-2d C++ - how to use or save the captured image?cocos-2d C++ 中的 captureScreen - 如何使用或保存捕获的图像?
【发布时间】:2017-10-18 19:14:17
【问题描述】:

我想在我的 (iOS) 应用程序中有一个按钮,用于截取当前屏幕的屏幕截图,然后将其附加到短信中。

就像我在其他应用程序中看到的那样......

我有消息发送工作,我认为我有截图工作,但我不知道截图保存在哪里或如何使用它。

我的消息发送是通过应用程序中的按钮调用的...

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());
}

截图功能是……

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");
}
}

我在控制台中收到消息“屏幕捕获成功”,然后我的消息应用程序打开并显示预填充的文本消息。

我需要做的是将屏幕截图添加到此消息中,但我看不到如何执行此操作,或者屏幕截图的保存位置,或者如何使用保存的屏幕截图。

【问题讨论】:

    标签: c++ cocos2d-x


    【解决方案1】:

    如果成功,它将保存在私有应用程序目录中。使用 iExplorer 之类的软件,找到您的应用程序,它应该在 Documents 目录中。如果您想在照片中看到它,您可以手动添加它。

    为了实现这一点,您必须调用 UIImageWriteToSavedPhotosAlbum

    见:How to save picture to iPhone photo library?

    您必须在 AppController.m 中创建一个函数并从此处使用它。您可以从 c++ 调用 objc 代码。它需要 UIImage,所以首先你必须传递文件路径并从中加载 uiimage,然后最后添加到图库。

    【讨论】:

    • 我最终想要对这张截图做的是将它发送到 iPhone 消息应用程序,以便它可以作为文本发送出去。我将如何将屏幕截图发送到消息?
    【解决方案2】:

    你可以使用这样的东西来获取图像保存的路径:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多