cocos creator2.1 截屏

cocos creator2.1 截屏

 //  战绩截图分享
    shareResult() {
        let size = cc.winSize;
        let width = Math.floor(size.width);
        let height = Math.floor(size.height);
        if (CC_JSB) {
            let fileName = "share.jpg";
            let filePath = jsb.fileUtils.getWritablePath() + fileName;
            if (jsb.fileUtils.isFileExist(filePath)) {
                jsb.fileUtils.removeFile(filePath);
            }
            let cameraNode = new cc.Node();
            cameraNode.parent = cc.director.getScene(); 
            let camera = cameraNode.addComponent(cc.Camera);
            cameraNode.position = cc.v2(width / 2, height / 2)
            camera.cullingMask = 0xffffffff;
            let texture = new cc.RenderTexture();
            texture.initWithSize(width, height, cc.gfx.RB_FMT_S8);
            camera.targetTexture = texture;
            camera.render();
            let data = texture.readPixels();
            //以下代码将截图后默认倒置的图片回正
            let picData = new Uint8Array(width * height * 4);
            let rowBytes = width * 4;
            for (let row = 0; row < height; row++) {
                let srow = height - 1 - row;
                let start = Math.floor(srow * width * 4);
                let reStart = row * width * 4;
                for (let i = 0; i < rowBytes; i++) {
                    picData[reStart + i] = data[start + i];
                }
            }
            //可以本地测试查看
			//let spriteFrame = new cc.SpriteFrame();
        	//spriteFrame.setTexture(texture);
        	//this.testSprite.spriteFrame = spriteFrame
	
            //保存图片
            let success = jsb.saveImageData(picData, width, height, filePath);
            console.log("截图成功,图片保存在 ====>", filePath);  
            if (success) {
                console.log("保存图像数据成功,文件: " + filePath);
            }
            else {
                console.error("保存图像数据失败!");
            }
          
        }
    },

你可能感兴趣的:(cocos,creator)