Cocos creator 原生截屏方案
2019-02-25 本文已影响1人
洒一地阳光_217d
功能比较简单,就不多解释了,看注释即可。
// 注意,EditBox,VideoPlayer,Webview 等控件无法被包含在截图里面
// 因为这是 OpenGL 的渲染到纹理的功能,上面提到的控件不是由引擎绘制的
let size = cc.director.getWinSize();
let fileName = "result_share.jpg";
let fullPath = jsb.fileUtils.getWritablePath() + fileName;
if (jsb.fileUtils.isFileExist(fullPath)) {
jsb.fileUtils.removeFile(fullPath);
}
// 若戴截图的场景中包含mask组件,则使用下面这一句:
// new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height), cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);//36168
let texture = new cc.RenderTexture(Math.floor(size.width), Math.floor(size.height));
texture.begin();
// 如果要针对某一个节点截图,则使用: a.node._sgNode.visit()
cc.director.getRunningScene().visit();
texture.end();
texture.setPosition(size.width / 2, size.height / 2);
texture.saveToFile(fileName, cc.IMAGE_FORMAT_PNG, true, function () {
console.log("截屏成功");
console.log("fullPath: " + fullPath);
});