看了很多截屏的小伙伴咨询问题,下面出下我们的做法,可以解决Mask问题
let node = new cc.Node();
node.parent = cc.director.getScene();
node.width = cc.view.getVisibleSize().width;
node.height = cc.view.getVisibleSize().height;
console.log(`!!!!!! capture width:${node.width} node.height:${node.height}`);
//注意了,这里要讲节点的位置改为右上角,不然截图只有1/4
node.x = node.width/2;
node.y = node.height/2;
let camera = node.addComponent(cc.Camera);
// 设置你想要的截图内容的 cullingMask
camera.cullingMask = 0xffffffff;
// 新建一个 RenderTexture,并且设置 camera 的 targetTexture 为新建的 RenderTexture,这样 camera 的内容将会渲染到新建的 RenderTexture 中。
let texture = new cc.RenderTexture();
texture.initWithSize(node.width, node.height, cc.gfx.RB_FMT_S8);
camera.targetTexture = texture;
// 渲染一次摄像机,即更新一次内容到 RenderTexture 中
//node.parent.scaleY = -1;
// 截图默认是y轴反转的,渲染前需要将图像倒过来,渲染完倒回去
camera.render();
//node.parent.scaleY = 1;
// 这样我们就能从 RenderTexture 中获取到数据了
let data = texture.readPixels();
let {width} = texture;
let {height} = texture;
let picData = this.filpYImage(data, width, height);
//做个截屏的动作
//this.captureAction(node, width, height);
let fileDir = `${jsb.fileUtils.getWritablePath()}cocosCapture`;
let fileName = `${this.format(new Date().getTime())}.png`;
let fullPath = `${fileDir}/${fileName}`;
if (!jsb.fileUtils.isDirectoryExist(fileDir)) {
jsb.fileUtils.createDirectory(fileDir);
}
if (jsb.fileUtils.isFileExist(fullPath)) {
jsb.fileUtils.removeFile(fullPath);
}
let success = jsb.saveImageData(picData, width, height, fullPath);
node.destroy();
if (success) {
// eslint-disable-next-line no-console
console.log('截屏成功,fullPath,width,height = ', fullPath, width, height);
captureNative(fullPath);
}
else {
cc.error('截屏失败!');
}