使用 PhoneGap 将 iPhone 相机胶卷中的图像发布到 Facebook 墙

发布于 2024-10-14 21:08:32 字数 1692 浏览 9 评论 0原文

我正在使用 PhoneGapiPhoneFaceBook 墙发布图像

我可以登录 FaceBook 帐户并发布托管图像 (http://www.mysite.com/my_image.jpg),但不能发布来自 iPhone 的图像。

以下是发布到 FB 的脚本:

function fbPost() {
    $.ajax({
           type: 'POST',
           url: "https://graph.facebook.com/me/feed",
           data: {
                message: "<FACEBOOK MESSAGE>",
                PICTURE: "<IMAGE URL>",
                name: "<TITLE OF POST>",
                link: "<LINK TO APP>",
                caption: "<SHOWN BELOW TITLE>",
                description: "<SHOWN BELOW CAPTION>",
                access_token: access_token,
                format: "json"
           },
           success: function (data) {
               navigator.notification.alert("success!", null, "Thanks!")
           },
                   },
           dataType: "json",
           timeout: 10000
           })
}

以下是从 iPhone 获取图像(相机胶卷或相册)的代码:

navigator.camera.getPicture(onSuccess, onFail, { quality: 50, 
    destinationType: Camera.DestinationType.FILE_URI }); 

function onSuccess(imageURI) {
    var image = document.getElementById('myImage');
    image.src = imageURI;
}

(http://docs.phonegap.com/phonegap_camera_camera.md.html)。

当我使用 iPhone 中的图像时,图像的 URI 类似于: file:///var/mobile/Applications/..../Documents/tmp/photo_001.jpg

同样,当我指定托管 (http: //...) 图像,但当它是来自 iPhone 的图像时则不然。

我将非常感谢任何帮助。

谢谢。

I am using PhoneGap to publish an image to a FaceBook wall from an iPhone.

I am able to log in a FaceBook account and publish a HOSTED image (http://www.mysite.com/my_image.jpg) but not an image from the iPhone.

Here is the script to post to FB:

function fbPost() {
    $.ajax({
           type: 'POST',
           url: "https://graph.facebook.com/me/feed",
           data: {
                message: "<FACEBOOK MESSAGE>",
                PICTURE: "<IMAGE URL>",
                name: "<TITLE OF POST>",
                link: "<LINK TO APP>",
                caption: "<SHOWN BELOW TITLE>",
                description: "<SHOWN BELOW CAPTION>",
                access_token: access_token,
                format: "json"
           },
           success: function (data) {
               navigator.notification.alert("success!", null, "Thanks!")
           },
                   },
           dataType: "json",
           timeout: 10000
           })
}

Here is the code to get an image from the iPhone (camera roll or album):

navigator.camera.getPicture(onSuccess, onFail, { quality: 50, 
    destinationType: Camera.DestinationType.FILE_URI }); 

function onSuccess(imageURI) {
    var image = document.getElementById('myImage');
    image.src = imageURI;
}

(http://docs.phonegap.com/phonegap_camera_camera.md.html).

When I use an image from the iPhone, the URI of the image is something like:
file:///var/mobile/Applications/..../Documents/tmp/photo_001.jpg

Again, I am able to publish an image when I specify a hosted (http://...) image, but not when it's an image from the iPhone.

I would greatly apreciate any help.

Thank you.

Rob

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

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

发布评论

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

评论(2

岁月蹉跎了容颜 2024-10-21 21:08:32

Facebook 无法访问您手机上的本地文件。我不知道如何将照片上传到 Facebook,但正如您之前所说,如果您使用公共托管文件,它就可以工作。

所以我认为你在这里有两个选择:
将照片上传到服务器,然后将文件的 URL 发布到 Facebook。 (你可以使用phonegap的文件api来做到这一点,有一个上传插件的事件),但我认为这并不是一个很好的解决方案。

我建议您查找是否可以将图像数据发布到facebook(可能是base64编码)并使用phonegap api获取照片的base64编码内容并将其直接发布到facebook

Facebook can not access a local file on your telephone. I dont know about the ways you can upload a photo to facebook, but as you said before it works if you use a public hosted file.

So I think you got two choices here:
Upload the foto to a server an then post the url of the file to facebook. (you can use the file api of phonegap to do that, theres event an upload plugin), but i think this is not really a good solution.

I would suggest to find if you can post the image data to facebook (maybe base64 encoded) and use the phonegap api to get the base64 encoded content of the foto and post this directly to facebook

懒猫 2024-10-21 21:08:32

我编写了一个 PhoneGap 插件,使用 Graph API 将 iPhone/iPad 中的照片发布到Facebook。该插件还可以发布带有 Facebook 地点 ID 的照片(意味着它可以使用照片签入 Facebook),并且在 iOS 6.0 上运行良好。

它基于 PhoneGap 2.1 和 phonegap-plugin-facebook-connect,以及 不需要任何中间主机,也许它可以解决你的问题;)

首先,PhotoCheckin.h是...

#import <Foundation/Foundation.h>
#import "Facebook.h"
#import "FBConnect.h"

#import <Cordova/CDV.h>

@interface PhotoCheckin : CDVPlugin
<FBDialogDelegate>

- (void) sendPost:(CDVInvokedUrlCommand*)command;

@end

其次,PhotoCheckin.m如下。

#import "PhotoCheckin.h"
#import "FBSBJSON.h"

@implementation PhotoCheckin

- (void) sendPost:(CDVInvokedUrlCommand*)command {
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[command.arguments objectAtIndex:0]]]];
    NSString* message = [command.arguments objectAtIndex:1];
    NSString* place_id = [command.arguments objectAtIndex:2];
    NSLog(@"%@",message);

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"source",message,@"message",place_id, @"place", nil];

    if (FBSession.activeSession.isOpen) {
        [FBRequestConnection startWithGraphPath:@"me/photos"
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  NSString *resultStr = nil;
                                  if (error) {
                                      //resultStr = [NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code];
                                      resultStr = [[CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]] toErrorCallbackString:command.callbackId];
                                  }else{
                                      //resultStr = [NSString stringWithFormat:@"Posted action, id: %@",[result objectForKey:@"id"]];
                                      resultStr = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[NSString stringWithFormat:@"Posted action, id: %@",[result objectForKey:@"id"]]] toSuccessCallbackString:command.callbackId];
                                  }
                                  //[[[UIAlertView alloc] initWithTitle:@"Check-in Result" message:resultStr delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                                  [self writeJavascript:resultStr];
                              }];
    }else{
        NSLog(@"no active session");

        NSArray *permissions = [[NSArray alloc] initWithObjects:@"publish_stream",nil];
        // more "permissions strings" at http://developers.facebook.com/docs/authentication/permissions/

        // OPEN Session
        [FBSession openActiveSessionWithPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) {
                                      if (FB_ISSESSIONOPENWITHSTATE(status)) {
                                          [[[UIAlertView alloc] initWithTitle:@"Got FB session!" message:@"please check-in again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                                      }else{
                                          [self writeJavascript:[[CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]] toErrorCallbackString:command.callbackId]];
                                          /*[[[UIAlertView alloc] initWithTitle:@"Login Fail"
                                                                      message:[NSString stringWithFormat:
                                                                               @"error: domain = %@, code = %d",
                                                                               error.domain, error.code]
                                                                     delegate:self
                                                            cancelButtonTitle:@"OK"
                                                            otherButtonTitles:nil]
                                           show];*/
                                      }
                                  }];
    }
}


@end

注:该方法发布时会回调,部分代码行被注释掉,可以提醒用户。

第三,PhotoCheckin.js如下。

var PhotoCheckin = {
    sendPost: function(photo_uri, message, place_id, success, fail) {
        return Cordova.exec(success, fail, "PhotoCheckin", "sendPost", [photo_uri, message, place_id]);
    }
}

最后我们去使用插件的时候,编写js如下。 (在 .html/.js 中)

PhotoCheckin.sendPost(photo_uri, message, place_id,
    function(result){
        navigator.notification.confirm(
            'message: '+result,
            null,
            'Photo Posted',
            'OK'
        );
    },
    function(error){
        navigator.notification.confirm(
            'message: '+error,
            null,
            'Photo Fail',
            'OK'
        );
    }
);

注意:photo_uri 是照片的 uri(例如 file:///...),message 是用户的评论,place_id 是 Facebook 中的地点 ID(例如 106522332718569)。

与其他插件一样,我们必须编辑 Resources/Cordova.plist,在插件中添加 photoCheckin 作为键,添加 PhotoCheckin 作为值。并将PhotoCheckin.hPhotoCheckin.m放入名为Plugins的文件夹中,在您的文件中包含PhotoCheckin.js文件js 代码。

玩得开心!来自台湾:)

I write a PhoneGap plug-in use Graph API to post photos from iPhone/iPad to Facebook. This plug-in can also post photo with Facebook place id (means it can Checkin Facebook with photo) and works fine on iOS 6.0.

It's based on PhoneGap 2.1 and phonegap-plugin-facebook-connect, and not need any middle host, maybe it can solve your problem ;)

First, the PhotoCheckin.h is...

#import <Foundation/Foundation.h>
#import "Facebook.h"
#import "FBConnect.h"

#import <Cordova/CDV.h>

@interface PhotoCheckin : CDVPlugin
<FBDialogDelegate>

- (void) sendPost:(CDVInvokedUrlCommand*)command;

@end

Second, PhotoCheckin.m as follow.

#import "PhotoCheckin.h"
#import "FBSBJSON.h"

@implementation PhotoCheckin

- (void) sendPost:(CDVInvokedUrlCommand*)command {
    UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[command.arguments objectAtIndex:0]]]];
    NSString* message = [command.arguments objectAtIndex:1];
    NSString* place_id = [command.arguments objectAtIndex:2];
    NSLog(@"%@",message);

    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:image, @"source",message,@"message",place_id, @"place", nil];

    if (FBSession.activeSession.isOpen) {
        [FBRequestConnection startWithGraphPath:@"me/photos"
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {
                                  NSString *resultStr = nil;
                                  if (error) {
                                      //resultStr = [NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code];
                                      resultStr = [[CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]] toErrorCallbackString:command.callbackId];
                                  }else{
                                      //resultStr = [NSString stringWithFormat:@"Posted action, id: %@",[result objectForKey:@"id"]];
                                      resultStr = [[CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:[NSString stringWithFormat:@"Posted action, id: %@",[result objectForKey:@"id"]]] toSuccessCallbackString:command.callbackId];
                                  }
                                  //[[[UIAlertView alloc] initWithTitle:@"Check-in Result" message:resultStr delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                                  [self writeJavascript:resultStr];
                              }];
    }else{
        NSLog(@"no active session");

        NSArray *permissions = [[NSArray alloc] initWithObjects:@"publish_stream",nil];
        // more "permissions strings" at http://developers.facebook.com/docs/authentication/permissions/

        // OPEN Session
        [FBSession openActiveSessionWithPermissions:permissions
                                       allowLoginUI:YES
                                  completionHandler:^(FBSession *session,
                                                      FBSessionState status,
                                                      NSError *error) {
                                      if (FB_ISSESSIONOPENWITHSTATE(status)) {
                                          [[[UIAlertView alloc] initWithTitle:@"Got FB session!" message:@"please check-in again." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
                                      }else{
                                          [self writeJavascript:[[CDVPluginResult resultWithStatus:CDVCommandStatus_JSON_EXCEPTION messageAsString:[NSString stringWithFormat:@"error: domain = %@, code = %d",error.domain, error.code]] toErrorCallbackString:command.callbackId]];
                                          /*[[[UIAlertView alloc] initWithTitle:@"Login Fail"
                                                                      message:[NSString stringWithFormat:
                                                                               @"error: domain = %@, code = %d",
                                                                               error.domain, error.code]
                                                                     delegate:self
                                                            cancelButtonTitle:@"OK"
                                                            otherButtonTitles:nil]
                                           show];*/
                                      }
                                  }];
    }
}


@end

Notes: this method will callback when posted, some code lines were commented out, which can make alert to users.

Third, the PhotoCheckin.js as follow.

var PhotoCheckin = {
    sendPost: function(photo_uri, message, place_id, success, fail) {
        return Cordova.exec(success, fail, "PhotoCheckin", "sendPost", [photo_uri, message, place_id]);
    }
}

Finally, when we went to use the plug-in, we write js as follow. (in .html/.js)

PhotoCheckin.sendPost(photo_uri, message, place_id,
    function(result){
        navigator.notification.confirm(
            'message: '+result,
            null,
            'Photo Posted',
            'OK'
        );
    },
    function(error){
        navigator.notification.confirm(
            'message: '+error,
            null,
            'Photo Fail',
            'OK'
        );
    }
);

Notes: photo_uri is the photo's uri (e.g. file:///...), message is user's comment and place_id is the place id in Facebook (e.g. 106522332718569).

And like other plug-ins, we must to edit Resources/Cordova.plist, add photoCheckin as key and PhotoCheckin as value in plugins. And put PhotoCheckin.h&PhotoCheckin.m to folder named Plugins, include PhotoCheckin.js file in your js code.

have fun! from 台灣 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文