使用 Facebook Graph Api 上传 BitmapData

发布于 2024-10-15 08:14:50 字数 2208 浏览 1 评论 0原文

我一直在互联网上查找如何使用 Facebook 在其 Graph Api 中提供的 PostRequest 类,但我找不到任何内容。我想知道如何使用它来上传位图数据。它看起来根本就没有动。

我使用了这段代码并且它起作用了:

private function getSnapshot(e:MouseEvent):void 
{
    bmd = new BitmapData(vidDisplay.width, vidDisplay.height); 
    bmd.draw(vidDisplay);
    vidDisplay.previewImg.source = new Bitmap(bmd);

    isSnapshot = true;

    vidDisplay.upload_btn.enabled = true;
    vidDisplay.caption_txt.enabled = true;
    vidDisplay.browse_btn.enabled = false;
}

protected function upload(w:MouseEvent):void 
{
    var img:* = (isSnapshot) ? bmd : fileRef ;
    var values:Object = {message:vidDisplay.caption_txt.text, fileName:'FILE_NAME', image:img};

    fb.callApi("post", "me", "photos", values);

    vidDisplay.caption_txt.text = "";
    vidDisplay.upload_btn.enabled = false;
    vidDisplay.snapShot_btn.enabled = false;
}

但是,当我尝试制作一个完整的函数来拍照并立即上传时,它不起作用。起初是这样的:

private function doTakeSnapshot(con:MovieClip, cap:String = null):void
{
    ExternalInterface.call("doAlert", "doTakeSnapshot");

    var bmd:BitmapData = new BitmapData(con.width, con.height);
    bmd.draw(con);

    if (cap == null) { cap = "Check out my photo!"; }
    var values:Object = {message:cap, image:bmd};

    doCallApi("post", "me", "photos", values);
}

然后我尝试使用PostRequest:

private function doTakeSnapshot(con:MovieClip, cap:String = null):void
{
    ExternalInterface.call("doAlert", "doTakeSnapshot");

    var bmd:BitmapData = new BitmapData(con.width, con.height);
    bmd.draw(con);

    if (cap == null) { cap = "Check out my photo!"; }

    var ba:ByteArray = PNGEncoder.encode(bmd);
    var pr:PostRequest = new PostRequest();
    //var ba:ByteArray = pr.getPostData();
    pr.createPostData();
    pr.writePostData("fileName", "testtest");
    pr.writeFileData("fileName", ba, "image/png");
    pr.close();

    var byteArray:ByteArray = pr.getPostData();
    ExternalInterface.call("doAlert", "ByteArray = " + byteArray);

    var values:Object = {message:cap, image:byteArray};

    doCallApi("post", "me", "photos", values);
}

两者都不起作用,这让我很困惑。第一个在没有以任何方式编码的情况下工作,所以我不明白为什么当我尝试这样调用它时它不起作用。有什么帮助吗?

I've been looking around the internet on how to use the PostRequest class given by the Facebook in their Graph Api, but I couldn't find anything on it. I'm wondering how to use it to upload a bitmapdata. It just doesn't seem to be moving at all.

I used this code and it worked:

private function getSnapshot(e:MouseEvent):void 
{
    bmd = new BitmapData(vidDisplay.width, vidDisplay.height); 
    bmd.draw(vidDisplay);
    vidDisplay.previewImg.source = new Bitmap(bmd);

    isSnapshot = true;

    vidDisplay.upload_btn.enabled = true;
    vidDisplay.caption_txt.enabled = true;
    vidDisplay.browse_btn.enabled = false;
}

protected function upload(w:MouseEvent):void 
{
    var img:* = (isSnapshot) ? bmd : fileRef ;
    var values:Object = {message:vidDisplay.caption_txt.text, fileName:'FILE_NAME', image:img};

    fb.callApi("post", "me", "photos", values);

    vidDisplay.caption_txt.text = "";
    vidDisplay.upload_btn.enabled = false;
    vidDisplay.snapShot_btn.enabled = false;
}

However, when I tried to make a whole function to take a picture and upload straightaway, it doesn't work. At first, it was like this:

private function doTakeSnapshot(con:MovieClip, cap:String = null):void
{
    ExternalInterface.call("doAlert", "doTakeSnapshot");

    var bmd:BitmapData = new BitmapData(con.width, con.height);
    bmd.draw(con);

    if (cap == null) { cap = "Check out my photo!"; }
    var values:Object = {message:cap, image:bmd};

    doCallApi("post", "me", "photos", values);
}

Then I tried with the PostRequest:

private function doTakeSnapshot(con:MovieClip, cap:String = null):void
{
    ExternalInterface.call("doAlert", "doTakeSnapshot");

    var bmd:BitmapData = new BitmapData(con.width, con.height);
    bmd.draw(con);

    if (cap == null) { cap = "Check out my photo!"; }

    var ba:ByteArray = PNGEncoder.encode(bmd);
    var pr:PostRequest = new PostRequest();
    //var ba:ByteArray = pr.getPostData();
    pr.createPostData();
    pr.writePostData("fileName", "testtest");
    pr.writeFileData("fileName", ba, "image/png");
    pr.close();

    var byteArray:ByteArray = pr.getPostData();
    ExternalInterface.call("doAlert", "ByteArray = " + byteArray);

    var values:Object = {message:cap, image:byteArray};

    doCallApi("post", "me", "photos", values);
}

Both doesn't work, which puzzles me. The first one worked without encoding it in any way, so I don't get why it doesn't work when I try to just call it like that. Any help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2024-10-22 08:14:50

您不必包装或编码您的位图数据。只需制作一个位图并将其放入您的对象中,如下所示:

var bmd:BitmapData = new BitmapData(con.width, con.height);
bmd.draw(con); 
var bitmap:Bitmap = new Bitmap(bm);
var values:Object = {message:cap, image:bitmap};
fb.callApi("post", "me", "photos", values);

You don't have to wrap or encode your bitmapdata. Just make a Bitmap and put it in your object like this :

var bmd:BitmapData = new BitmapData(con.width, con.height);
bmd.draw(con); 
var bitmap:Bitmap = new Bitmap(bm);
var values:Object = {message:cap, image:bitmap};
fb.callApi("post", "me", "photos", values);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文