麒麟子Cocos Creator实用技巧四:打包原生App截图白屏解决方案

大家在做棋牌App或者一些特定需求的时候,需要截取当前游戏屏幕内容保存。

我们一般是采用cc.RenderTexture来截图并保存到游戏的可写目录

有时候会遇上,截出来的图片是白屏,或者部分白屏。

经过多方测试,我们发现,是Mask的锅,用了cc.Mask的界面,截图的时候,就会遇上这样的问题。

如果遇上这样的问题,只需要检查你的cc.RenderTexture初始化的时候,是否少了参数。最主要的是第三个参数,一定要是RGBA8888

var texture = new cc.RenderTexture(w, h, cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);

完整示例如下:

function captureScreen(){
    var size = cc.director.getWinSize();
    var fileName = "result_share.jpg";
    var fullPath = jsb.fileUtils.getWritablePath() + fileName;
    if (jsb.fileUtils.isFileExist(fullPath)) {
        jsb.fileUtils.removeFile(fullPath);
    }
    var width = Math.floor(size.width);
    var height = Math.floor(size.height);
    var texture = new cc.RenderTexture();
    texture.initWithSize(width, height, cc.gfx.RB_FMT_D24S8);
    texture.setPosition(cc.p(size.width / 2, size.height / 2));
    texture.begin();
    cc.director.getRunningScene().visit();
    texture.end();
    texture.saveToFile(fileName, cc.IMAGE_FORMAT_JPG);
}    
发布了230 篇原创文章 · 获赞 542 · 访问量 118万+

猜你喜欢

转载自blog.csdn.net/qq_36720848/article/details/89501956