How to Save a Screenshot

本文介绍了一种在v3.2alpha0版本中新增的截图保存方法。通过调用utils::captureScreen()函数,可以将屏幕截图并保存到指定路径。文章详细解释了此函数的参数及其用法,并提供了C++与Lua两种语言的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值