quick cocos 截屏并保存

调用cc.RenderTexture 即可。

    --需要截的屏幕大小
    local render_texture = cc.RenderTexture:create(640, 1136)
    --开始截屏
    render_texture:begin()
    --截self.node_container包含的内容
    self.node_container:visit()
    --关闭
    render_texture:endToLua()
    --调用
    local photo_texture = render_texture:getSprite():getTexture()
    local sprite_photo = cc.Sprite:createWithTexture(photo_texture)
    --截屏后的sprite_photo为原始图片沿y轴翻转后的样子。若需要原图,调用如下函数.
    sprite_photo:flipY()
    local result = render_texture:saveToFile("share.png", cc.IMAGE_FORMAT_PNG)
    -- saveToFile函数会默认添加根路径。
    if not result then
        print("save file failed")
    end

注:若需要让某些图片强制渲染,调用sprite:visit()方法。

//C++对应的RenderTexture类的saveToFile方法。
bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA)  
{  
    CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG,  
             "the image can only be saved as JPG or PNG format");  
    if (isRGBA && format == Image::Format::JPG) CCLOG("RGBA is not supported for JPG format");  

    //保存图片文件的路径  
    std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;  
    //初始化将纹理保存到文件的自定义渲染指令  
    _saveToFileCommand.init(_globalZOrder);  
    //设置自定义渲染指令的回调函数  
    _saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA);  

    Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand);  
    return true;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值