How to Save a Screenshot

来源:互联网 发布:浴缸 知乎 编辑:程序博客网 时间:2024/04/30 12:53

How to Save a Screenshot

On v3.2 alpha0, utils::captureScreen() is added to save a screenshot.

The declaration of utils::captureScreen() is

1
void captureScreen(const std::function<void(bool, const std::string&)>& afterCaptured, const std::string& filename);
  • afterCapturedThis function will be called after capturing commands are executed. The boolean return value indicates screenshot is captured successfully or not. The string return value is the path screenshot is saved in.
  • filenameThe name of screenshot. It can be just a filename, such as screenshot.png. It can also be a resolute path, such as/sdcard/screenshot.png.

The declaration of utils::captureScreen() for Lua is:

1
cc.utils:captureScreen(capturedHandler, filename)
  • capturedHandler This function will be called after capturing commands are executed. The format of the captureHandler as follows:
123
local function capturedHandler(succeed, outputFile)           --do somethingend

The succeed return value indicates screenshot is captured successfully or not. The stringoutputFile value is the path screenshot is saved in.

  • filenameThe name of screenshot. It can be just a filename, such as screenshot.png. It can also be a resolute path, such as/sdcard/screenshot.png.

Usage

123456789101112131415161718192021
void CaptureScreenTest::afterCaptured(bool succeed, const std::string& outputFile){    if (succeed)    {        // show screenshot        auto sp = Sprite::create(outputFile);        addChild(sp, 0, childName);        Size s = Director::getInstance()->getWinSize();        sp->setPosition(s.width / 2, s.height / 2);        sp->setScale(0.25);    }    else    {        log("Capture screen failed.");    }}void CaptureScreenTest::capture(){    utils::captureScreen(CC_CALLBACK_2(CaptureScreenTest::afterCaptured, this),"CaptureScreenTest.png");}
123456789101112131415161718
 local function afterCaptured(succeed, outputFile)      if succeed then           local sp = cc.Sprite:create(outputFile)           layer:addChild(sp, 0, childTag)           local winSize =  cc.Director:getInstance():getWinSize()           sp:setPosition(winSize.width / 2, winSize.height / 2)           sp:setScale(0.25)      else           cclog("Capture screen failed.")      endendlocal function onCaptured(tag, sender)      cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName)      layer:removeChildByTag(childTag)      fileName = "CaptureScreenTest.png"      cc.utils:captureScreen(afterCaptured, fileName)end
0 0
原创粉丝点击