游戏截图转base64在微信报错了

  • Creator 版本: 3.8

  • 目标平台:微信小游戏

在浏览器上正常,微信报错也看不出哪里问题,

public static captureNodeToSpriteFrame(targetNode: Node, camera?: Camera) {
try {
if (!targetNode || !targetNode.isValid) {
console.error(‘无效的节点’);
return null;
}
const nodeTransform = targetNode.getComponent(UITransform);
if (!nodeTransform) {
console.error(‘节点缺少UITransform组件’);
return null;
}
const nodeSize = nodeTransform.contentSize;
if (nodeSize.width <= 0 || nodeSize.height <= 0) {
console.error(‘节点尺寸无效’);
return null;
}

        // 创建渲染纹理
        const renderTexture = new RenderTexture();
        const size = {
            width: Math.ceil(nodeSize.width),
            height: Math.ceil(nodeSize.height)
        };

        renderTexture.reset({
            width: size.width,
            height: size.height
        });

        // 创建临时相机
        let tempCamera: Camera = null!;
        if (!camera) {
            const cameraNode = new Node('TempCamera');
            tempCamera = cameraNode.addComponent(Camera);
            tempCamera.clearFlags = Camera.ClearFlag.SOLID_COLOR;
            tempCamera.clearColor = new Color(0, 0, 0, 0);
            tempCamera.projection = Camera.ProjectionType.ORTHO;
            // 将相机添加到场景
            const scene = director.getScene();
            if (scene) {
                scene.addChild(cameraNode);
            } else {
                console.error('场景不存在');
                return null;
            }

            // 计算相机位置
            const worldPos = new Vec3();
            targetNode.getWorldPosition(worldPos);
            // 设置相机位置和参数
            cameraNode.setWorldPosition(worldPos.x, worldPos.y, 1000);
            tempCamera.orthoHeight = nodeSize.height / 2;
            tempCamera.near = 0.1;
            tempCamera.far = 2000;
            // 确保相机能看到目标节点
            tempCamera.visibility = targetNode.layer;
        }

        const captureCamera = camera || tempCamera;
        // 设置渲染目标
        captureCamera.targetTexture = renderTexture;
        // 确保相机和节点都是激活的
        if (tempCamera) {
            tempCamera.node.active = true;
        }

        const originalActive = targetNode.active;
        targetNode.active = true;

        // 强制更新场景
        director.root!.frameMove(0);
        game.step();

        // 获取像素数据并转换为 Base64
        const pixels = renderTexture.readPixels()!;
        const canvas = document.createElement('canvas');
        canvas.width = size.width;
        canvas.height = size.height;
        const ctx = canvas.getContext('2d')!;
        const Uint8Data = new Uint8ClampedArray(pixels, size.width, size.height)
        //微信不支持imageData
        // const imageData = new ImageData(new Uint8ClampedArray(pixels), size.width, size.height);
        const imageData = ctx.getImageData(0, 0, size.width, size.height);
        for (let i = 0; i < Uint8Data.length; i++) {
            imageData.data[i] = Uint8Data[i];
        }
        ctx.putImageData(imageData, 0, 0);

        const base64 = canvas.toDataURL('image/jpeg', 0.1);  // 获取 Base64 字符串

        // 清理
        if (tempCamera) {
            tempCamera.targetTexture = null;
            tempCamera.node.destroy();
        }

        // 恢复节点状态
        targetNode.active = originalActive;
        // return spriteFrame;
        return base64;
    } catch (error) {
        console.error('截图过程出错:', error);
        return null;
    }

}

有没有大佬知道要怎么解决呢

已解决代码没问题,是base64转图片报错了