标签:cocos2d-x3.1 cocos2d-x3.2 截图功能
void saveScreenshot(const std::string& fileName,const std::function<void(const std::string&)>& callback);
void Director::saveScreenshot(const std::string &fileName, const std::function<void (const std::string &)> &callback) { Image::Format format; //进行后缀判断 if(std::string::npos != fileName.find_last_of(".")){ auto extension = fileName.substr(fileName.find_last_of("."),fileName.length()); if (!extension.compare(".png")) { format = Image::Format::PNG; } else if(!extension.compare(".jpg")) { format = Image::Format::JPG; } else{ log("cocos2d: the image can only be saved as JPG or PNG format"); return; } } else { log("cocos2d: the image can only be saved as JPG or PNG format"); return ; } //获取屏幕尺寸,初始化一个空的渲染纹理对象 auto renderTexture = RenderTexture::create(getWinSize().width, getWinSize().height, Texture2D::PixelFormat::RGBA8888); //清空并开始获取 renderTexture->beginWithClear(0.0f, 0.0f, 0.0f, 0.0f); //遍历场景节点对象,填充纹理到RenderTexture中 getRunningScene()->visit(); //结束获取 renderTexture->end(); //保存文件 renderTexture->saveToFile(fileName , format); //使用schedule在下一帧中调用callback函数 auto fullPath = FileUtils::getInstance()->getWritablePath() + fileName; auto scheduleCallback = [&,fullPath,callback](float dt){ callback(fullPath); }; auto _schedule = getRunningScene()->getScheduler(); _schedule->schedule(scheduleCallback, this, 0.0f,0,0.0f, false, "screenshot"); }
bool HelloWorld::init() { bool bRet = false; do{ CC_BREAK_IF(!Layout::init()); auto button = ui::Button::create("CloseNormal.png","CloseSelected.png"); button->setPosition(Vec2(200,200)); addChild(button); button->addTouchEventListener(CC_CALLBACK_2(HomeLayer::touchEvent,this)); Director::getInstance()->getEventDispatcher(); bRet = true; }while(0); return bRet; } void HelloWorld::touchEvent(cocos2d::Ref *pSender, cocos2d::ui::Widget::TouchEventType type) { switch (type) { case Widget::TouchEventType::ENDED: <span style="white-space:pre"> </span> //参数依次为保存图片的名字,在控制台打印保存路径信息 Director::getInstance()->saveScreenshot("homeLayer.png", [&](const std::string &str){ log("str = %s",str.c_str()); }); break; default: break; } }
void Util::captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename);
标签:cocos2d-x3.1 cocos2d-x3.2 截图功能
原文地址:http://blog.csdn.net/yuxikuo_1/article/details/39051447