cocos creator2.1 截屏

本文详细介绍了如何在Cocos Creator 2.1中实现游戏截图功能,包括关键步骤和代码实现,帮助开发者记录和分享游戏精彩瞬间。
摘要由CSDN通过智能技术生成

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("保存图像数据失败!");
            }
          
        }
    },
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值