分享一个真正能在2.0及以后截屏的函数,截图全0的进来看,任意场合调用

// This is a temporary solution
filpYImage (data, width, height) {
    // create the data array
    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 = srow * width * 4;
        let reStart = row * width * 4;
        // save the piexls data
        for (let i = 0; i < rowBytes; i++) {
            picData[reStart + i] = data[start + i];
        }
    }
    return picData;
}

//截屏
captureScreen(fileName:string,caller:Function):void{

    let camera = cc.director.getScene().getChildByName("Canvas").getChildByName("Main Camera").getComponent(cc.Camera);
    
    let texture = new cc.RenderTexture();
    let gl = cc.game._renderContext;
    texture.initWithSize(cc.visibleRect.width, cc.visibleRect.height, gl.STENCIL_INDEX8);
    camera.targetTexture = texture;
    camera.render();

    let data = texture.readPixels();
    let width = texture.width;
    let height = texture.height;
    let picData = this.filpYImage(data,width, height);

    camera.targetTexture = null;

    let fullPath = jsb.fileUtils.getWritablePath() + fileName;
    if (jsb.fileUtils.isFileExist(fullPath)) {
        jsb.fileUtils.removeFile(fullPath);
    }

    let success = jsb.saveImageData(picData, width, height, fullPath);
    if (success) {
        if (caller) {
            caller(null,fullPath);
        }
    }else {
        if (caller) {
            caller("save image data failed!",null);
        }
    }
}
6赞

mark

saveImageData 在有些版本没有