如何在iOS上的cocos2d-x 2.x中将屏幕截图上传到Facebook?

时间:2014-06-16 04:02:09

标签: ios cocos2d-x

我已经搜索但没有找到将屏幕截图上传到iOS上的Facebook的解决方案

在cocos2d-x中,gamescene.cpp:

void GameScene::takeScreenshot()
{
    SoundManager::getInstance()->playSound("sound2.mp3");
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height);
    texture->setPosition(ccp(size.width/2, size.height/2));
    texture->begin();
    CCDirector::sharedDirector()->getRunningScene()->visit();
    texture->end();
    texture->saveToFile("screenshot.png", kCCImageFormatPNG);

    Link::postToFacebook();

}
链接中的

:postToFacebook功能:

if([SLComposeViewController isAvailableForServiceType:SLServiceTypeFacebook]) {

        SLComposeViewController *controller = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeFacebook];

        [controller setInitialText:@"First post from my iPhone app"];
        [controller addURL:[NSURL URLWithString:@"http://www.jinibot.com"]];
//        [controller addImage:image];
        [controller addImage:[UIImage imageNamed:@"screenshot.png"]];


        [controller dismissViewControllerAnimated:YES completion:nil];

        //add as many images as you want
//        [controller presentViewController:mail animated:YES completion:nil];
    }

但它不起作用。

请帮帮我。谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码存在问题

[controller addImage:[UIImage imageNamed:@"screenshot.png"]];

此方法

texture->saveToFile("screenshot.png", kCCImageFormatPNG);

将图像保存在文档目录中,但是在这里您使用图像作为无法检索的图像图像。 因此,从UIImage目录路径创建NSDocuments对象。 代码将正常工作。 :)