需要用到的includde文件
#include "Cocos2dx/Common/CCUMSocialSDK.h"
#include "Cocos2dx/ShareButton/UMShareButton.h"#include "Cocos2dx/Common/CCUMTypeDef.h"
命名空间
USING_NS_UM_SOCIAL;
// 获取一个CCUMSocialSDK实例
CCUMSocialSDK *sdk = CCUMSocialSDK::create("5513ab19fd98c546f70004b5");
// 设置微信的app id和app key
sdk->setWeiXinAppInfo("wxde1945ff7a43a0d9",
"c70ff7b551871a93f2562ce0086c1c1f");
sdk->setQQAppIdAndAppKey("1104461258", "OJWtuamgL6IYKmVH");
vector<int>* platform2 = new vector<int>();
platform2->push_back(WEIXIN);
platform2->push_back(WEIXIN_CIRCLE) ;
platform2->push_back(QQ);
platform2->push_back(QZONE) ;
sdk->setPlatforms(platform2) ;
// 创建分享按钮, 参数1为按钮正常情况下的图片, 参数2为按钮选中时的图片,参数3为友盟appkey, 参数4为分享回调
UMShareButton *shareButton = UMShareButton::create("res/share.png","res/share_press.png", "5513ab19fd98c546f70004b5", share_selector(shareCallback)) ;
// 显示在友盟分享面板上的平台
vector<int>* platforms = new vector<int>();
platforms->push_back(WEIXIN);
platforms->push_back(WEIXIN_CIRCLE) ;
platforms->push_back(SINA);
platforms->push_back(QZONE) ;
platforms->push_back(QQ) ;
platforms->push_back(RENREN) ;
//platforms->push_back(DOUBAN) ;
// 设置友盟分享面板上显示的平台
shareButton->setPlatforms(platforms);
// 设置文本分享内容
shareButton->setShareContent("我在帮助史莱特重返色の星球,你也一起来吧!") ;
// 设置要分享的图片, 图片支持本地图片和url图片, 但是url图片必须以http://或者https://开头
if(saveScreenShot()){
String imagePath = CCFileUtils::sharedFileUtils()->getWritablePath() + "screenshot.jpg";
shareButton->setShareImage(imagePath.getCString()) ;
}
shareButton->setPosition(
Point(size.width - soundOnMenuItem->getContentSize().width / 2, soundOnMenuItem->getContentSize().height / 2));
shareButton->setScale(0.8);
shareButton->setTag(101);
menu->addChild(shareButton);
其中调用的截屏功能bool saveScreenShot()
bool MainScene::saveScreenShot()
{
if(!CCUserDefault::sharedUserDefault()->getBoolForKey("first", true)){
return true;
}
// ***************************************************************************
// 截屏
CCSize size = CCDirector::sharedDirector()->getWinSize();
CCLog("Window Size, width = %d, height = %d.", (int)size.width, (int)size.height );
CCRenderTexture* texture = CCRenderTexture::create((int)size.width, (int)size.height, kCCTexture2DPixelFormat_RGBA8888);
texture->setPosition(ccp(size.width/2, size.height/2));
texture->begin();
CCDirector::sharedDirector()->getRunningScene()->visit();
texture->end();
string imagePath = CCFileUtils::sharedFileUtils()->getWritablePath() ;
if ( imagePath.length() == 0 )
{
return false;
}
// imagePath = imagePath.c_str() ;
char imgName[20] = "screenshot.jpg" ;
//保存为jpeg
bool result = texture->saveToFile(imgName, kCCImageFormatJPEG);
if ( result )
{
// imagePath += "screenshot.png";
imagePath += imgName ;
}
CCUserDefault::sharedUserDefault()->setBoolForKey("first", false);
return true;
}
分享反馈
void shareCallback(int platform, int stCode, string& errorMsg)
{
if ( stCode == 100 )
{
CCLog("#### HelloWorld 开始分享");
}
else if ( stCode == 200 )
{
CCLog("#### HelloWorld 分享成功");
}
else
{
CCLog("#### HelloWorld 分享出错");
}
CCLog("platform num is : %d.", platform);
}