cocos2d-x 截屏相关的内容

作者 qiuzhong 日期 2017-12-18
cocos2d-x 截屏相关的内容

No matter what happens, even if the stars fall, I will live.

最近刚做完微信分享的接入,其中需要截屏并分享的功能
cocos2d 愉快的告诉我,朋友别造轮子啦,我们给你做好啦

local fileName ="test.png"
cc.utils:captureScreen(function(succeed, outputFile)
print("wechat share 1 ")
if succeed then
print("截屏成功")
else
print("截屏失败")
end
end, fileName)

是不是很爽,但是需求总是会变的,当需求变成分享出去的图片是场景里没有的,而且还是动态需要绘制后才行的
这时候心里有句话。。。

local fileName = "test.png"--路径最好放在SD卡上,不然Android会取不到图片的
local node_container = cc.Layer:create()-- 在layer上加自己想要的内容就可以啦
local render_texture = cc.RenderTexture:create(720,1280)
--开始截屏
render_texture:begin()
--截self.node_container包含的内容
node_container:visit()
--关闭
render_texture:endToLua()
--调用
local result = render_texture:saveToFile(fileName,cc.IMAGE_FORMAT_PNG)
--图片保存到本地,然后就可以调用分享啦