原文链接:http://www.it165.net/pro/html/201409/21451.html
1.1首先在CCDirector.h中添加如下代码:并在其中添加头文件 #include "2d/CCRenderTexture.h"
1.
void
saveScreenshot(
const
std::string& fileName,
std::function<
(
std::string&)>& callback);
1.2然后在CCDirector.cpp中添加如下代码:
01.
Director::saveScreenshot(std::string &fileName,宋体; background:none">std::string &)> &callback)
02.
{
03.
Image::Format format;
04.
05.
if
(std::string::npos != fileName.find_last_of(
"."
)){
06.
auto extension = fileName.substr(fileName.find_last_of(
),fileName.length());
07.
(!extension.compare(
".png"
)) {
08.
format = Image::Format::PNG;
09.
}
else
".jpg"
10.
format = Image::Format::JPG;
11.
else{
12.
log("cocos2d: the image can only be saved as JPG or PNG format"
);
13.
return
;
14.
}
15.
16.
17.
18.
19.
20.
auto renderTexture = RenderTexture::create(getWinSize().width,getWinSize().height,Texture2D::PixelFormat::RGBA8888);
22.
renderTexture->beginWithClear(0
.0f,
.0f);
23.
//遍历场景节点对象,填充纹理到RenderTexture中
24.
getRunningScene()->visit();
26.
renderTexture->end();
28.
renderTexture->saveToFile(fileName,format);
29.
//使用schedule在下一帧中调用callback函数
30.
auto fullPath = FileUtils::getInstance()->getWritablePath() + fileName;
31.
auto scheduleCallback = [&,fullPath,callback](float
dt){
32.
callback(fullPath);
33.
};
34.
auto _schedule = getRunningScene()->getScheduler();
35.
_schedule->schedule(scheduleCallback,宋体; background:none">this,宋体; background:none">false
"screenshot"
);
36.
37.
}