1

我正在为 facebook 在 cocos2d-JS 上制作一个游戏,其中需要共享游戏的屏幕截图。

我可以截取屏幕截图,但现在我无法将其上传到 Parse.com 服务器,因为它需要 base64 格式或字节数组。我无法找到将 Sprite 转换为这种格式的任何解决方案。这是我的代码,所以当我添加子时它的结果是正确的。我还添加了我的注释代码,以便它有助​​于理解我已经尝试了很多事情,但无法达到同样的效果。

shareToSocialNetworking: function () {

    cc.director.setNextDeltaTimeZero(true);

    var newsize = cc.director.getVisibleSize();

    var renderText = new cc.RenderTexture(newsize.width,newsize.height);

    renderText.begin();
    cc.director.getRunningScene().visit();
    renderText.end();
    var result = cc.Sprite.create(renderText.getSprite().getTexture());
    result.flippedY = true;
    this._mainViewNode.addChild(result,6000);

    //renderText.saveToFile("screenshot.jpg",cc.IMAGE_FORMAT_PNG);
    //var based = renderText.getSprite().getTexture().getStringForFormat().toString();
    //var data = based.getData();
    var file = new Parse.File("screen.jpg", { base64: this.getBase64(result) });
    //var file = new Parse.File("screen.jpg", data, "image/png");
    var self = this;

    file.save().then(function() {
        // The file has been saved to Parse.
        alert(file.url);
     this.onSharePictureInfoLink(file.url());

    }, function(error) {
        // The file either could not be read, or could not be saved to Parse.
    });


    //
    //var ccImage = renderText.newCCImage();
    //
    //var str = ccImage.getData();

},

有什么可以解决的方法吗

4

2 回答 2

1

这是从 cocos2d-JS 获取屏幕截图的方法

 screenshot: function (fileName) {
        var tex = new cc.RenderTexture(winSize.width, winSize.height, cc.Texture2D.PIXEL_FORMAT_RGBA8888);
        tex.setPosition(cc.p(winSize.width / 2, winSize.height / 2));
        tex.begin();
        cc.director.getRunningScene().visit();
        tex.end();

        var imgPath = jsb.fileUtils.getWritablePath();
        if (imgPath.length == 0) {
            return;
        }
        var result = tex.saveToFile(fileName, cc.IMAGE_FORMAT_JPEG);
        if (result) {
            imgPath += fileName;
            cc.log("save image:" + imgPath);
            return imgPath;
        }
        return "";
    }

然后从 Javascript 进行 Java 调用

 public static void ScreenShot()
    {
        Bitmap imageBitmap  = BitmapFactory.decodeFile(Cocos2dxHelper.getCocos2dxWritablePath() + "/" + "screenshot.png");

        String fileHolder = "SampleFolder";
        File filepathData = new File("/sdcard/" + fileHolder);

        //~~~Create Dir
        try {
                if (!filepathData.exists()) 
                {
                    filepathData.mkdirs();
                    filepathData.createNewFile();

                    FileWriter fw = new FileWriter(filepathData + fileHolder);
                    BufferedWriter out = new BufferedWriter(fw);
                    String toSave = String.valueOf(0);
                    out.write(toSave);
                    out.close();
                }
            } 
            catch (IOException e1) {

            }   

            //~~~Create Image
            File file = new File("/sdcard/" + "Your filename");

            try 
            {
                file.createNewFile();
                FileOutputStream ostream = new FileOutputStream(file);
                imageBitmap.compress(CompressFormat.PNG, 100, ostream);
                ostream.close();
            } 
            catch (Exception e) {}

            Uri phototUri = Uri.fromFile(file);
            Intent shareIntent = new Intent();
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, phototUri);
            //~~~Add Code Below
    }

不要忘记添加外部存储权限

于 2014-12-22T19:06:59.787 回答
1

有一个名为_cacheCanvas的私有变量,它是离屏画布的实例

你可以简单地做renderText._cacheCanvas.toDataURL()

于 2014-11-25T08:08:51.260 回答