How to Save a Screenshot

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:

 
 
1
2
3
local function capturedHandler(succeed, outputFile)
           --do something
end

The succeed return value indicates screenshot is captured successfully or not. The string outputFile 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


 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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");
}

 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 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.")
      end
end

local function onCaptured(tag, sender)
      cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName)
      layer:removeChildByTag(childTag)
      fileName = "CaptureScreenTest.png"
      cc.utils:captureScreen(afterCaptured, fileName)
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值