其他分享
首页 > 其他分享> > Creator 原生平台截屏分享

Creator 原生平台截屏分享

作者:互联网

百度一下其实有很多关于Creator 原生平台截屏的相关文章,但是为什么我还要写这篇文章呢?因为今天遇到一个大坑了。
网上其它文章中介绍的截图代码其实都是可用的,坑的就是在安卓平台下需要微信分享时出现问题了。
微信分享需要读取本地图片,但是通过查看CCRenderTexture.cpp得知renderTexture.saveToFile保存的文件其实是在你游戏应用目录下的,微信读取不到。所以会造成能拉起微信分享,但是一直显示“正在发送中”的情况。

bool RenderTexture::saveToFile(const std::string& fileName, Image::Format format, bool isRGBA, std::function<void (RenderTexture*, const std::string&)> callback)
{
    CCASSERT(format == Image::Format::JPG || format == Image::Format::PNG,
             "the image can only be saved as JPG or PNG format");
    if (isRGBA && format == Image::Format::JPG) CCLOG("RGBA is not supported for JPG format");

    _saveFileCallback = callback;

std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName;
_saveToFileCommand.init(_globalZOrder);
_saveToFileCommand.func = CC_CALLBACK_0(RenderTexture::onSaveToFile, this, fullpath, isRGBA);

Director::getInstance()->getRenderer()->addCommand(&_saveToFileCommand);
return true;
}

所以需要把截图保存的文件另外保存一次
代码如下:

var getScreenShotImagePath = function(){
    var fullPath = jsb.fileUtils.getWritablePath() + 'ScreenShotShare.png'; //拿到可写路径,将图片保存在本地,可以在ios端或者java端读取该文件
    if(cc.sys.os == cc.sys.OS_ANDROID){
        fullPath = '/sdcard/ScreenShotShare.png';
    }
    if (jsb.fileUtils.isFileExist(fullPath)) {  
        jsb.fileUtils.removeFile(fullPath);  
    }
    return fullPath;
}
/**
 * shareNode 要截图的节点
 * hideNodes 要隐藏的节点列表
 * hasMask   是否含有Mask
 */
var shareScreenShoot = function(shareNode, hideNodes, hasMask){    
    if(!cc.sys.isNative){
        return;
    }    
    //如果待截图的场景中含有 mask,请使用下面注释的语句来创建 renderTexture
    var renderTexture;
    if(!hasMask){
        renderTexture = cc.RenderTexture.create(shareNode.width,shareNode.height);
    }else{
        renderTexture = cc.RenderTexture.create(shareNode.width,shareNode.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);
    }
    //实际截屏的代码
    var position = shareNode.position;
    renderTexture.begin();
    shareNode.position = cc.p(shareNode.width/2,shareNode.height/2);
    var setBtnVisible = function(hideNodes, active){
        for(var index in hideNodes){
            hideNodes[index].active = active;
        }
    }
    setBtnVisible(hideNodes);
    shareNode._sgNode.visit();
    renderTexture.end();
    var imagePath = getScreenShotImagePath();
    //saveToFile 是放在jsb.fileUtils.getWritablePath()的路径中,只能传入文件名。传不了路径
    renderTexture.saveToFile("ScreenShotShare.png",cc.ImageFormat.PNG, true, function () {
        if (jsb.fileUtils.isFileExist(jsb.fileUtils.getWritablePath() + "ScreenShotShare.png")) {
            if(cc.sys.os == cc.sys.OS_ANDROID){//安卓的分享路径比较坑,只能重新写文件
                var fileData = jsb.fileUtils.getDataFromFile(jsb.fileUtils.getWritablePath() + "ScreenShotShare.png");
                jsb.fileUtils.writeDataToFile(fileData, imagePath);
                jsb.fileUtils.removeFile(jsb.fileUtils.getWritablePath() + "ScreenShotShare.png");//写了新文件后删除旧文件
            }
            //shareImage(imagePath);//微信分享接口,需要自己实现
        }
        shareNode.position = position;
        setBtnVisible(hideNodes, true);
    });
}

 

使用方法:

    onShareClick(){
        var btnList = [];
        btnList[btnList.length] = cc.find("Close", this.node);
        btnList[btnList.length] = cc.find("Btn_share", this.node);
        gamemain.shareScreenShoot(this.node, btnList);
    }

 

 

 

 

 

 

 

 

标签:原生,renderTexture,jsb,Creator,cc,shareNode,var,截屏,fileUtils
来源: https://blog.csdn.net/ccnu027cs/article/details/101299115