加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

quick cocos 截屏并保存

发布时间:2020-12-14 17:22:13 所属栏目:百科 来源:网络整理
导读:调用cc.RenderTexture 即可。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 --需要截的屏幕大小 local render_texture = cc.RenderTexture: create ( 640 , 1136 ) --开始截屏 render_texture:begin() --截s

调用cc.RenderTexture 即可。

   
   
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
--需要截的屏幕大小 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()方法。

    
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    //C++对应的RenderTexture类的saveToFile方法。 bool RenderTexture::saveToFile(const std::string& fileName,Image::Format format,bool isRGBA) { CCASSERT(format == Image::Format::JPG || format ::PNG,"the image can only be saved as JPG or PNG format"); if (isRGBA && 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->getRenderer()->addCommand(&_saveToFileCommand); return true; }

    (编辑:李大同)

    【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

      推荐文章
        热点阅读