2011-01-31 6 views
0

でiPhoneのカメラロールからFacebookの壁に画像を投稿私はiPhoneからFacebookの壁画像を公開するPhoneGapのを使用しています。は、PhoneGapの

私はFaceBookアカウントにログインし、HOSTED画像(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からのイメージです。

私はどんな助けも大いに評価します。

ありがとうございます。

ロブ

答えて

3

あなたの電話のローカルファイルにアクセスすることはできません。 Facebookに写真をアップロードする方法についてはわかりませんが、あなたがパブリックのホストされたファイルを使用する場合は、それが動作する前に言ったように。

ここでは2つの選択肢があると思います。 fotoをサーバーにアップロードしてから、ファイルのURLをFacebookに投稿してください。 (あなたはそれを行うためにphonegapのファイルAPIを使用することができます、theresイベントアップロードプラグイン)、私はこれが本当に良い解決策ではないと思う。

私は私が書くあなたがFacebookのに画像データを投稿することができた場合に見つけることが示唆(多分Base64エンコード)して写真をBase64エンコードされたコンテンツを取得し、Facebookの

+0

ダニエルありがとう。 PhoneGap APIには、キャプチャされたイメージをbase64でエンコードされた文字列として取得するオプションがあります: 'document.getElementById( 'image')。src =" data:image/jpeg; base64、 "+ imageData;' 。少なくとも私は '' picture: "document.getElementById( 'image')。src" ' –

+0

を書きました。FacebookのAPIを素早く見て、URLだけを受け取りました。画像をアップロードする必要があります。これを行う良い方法はここで見つけることができます:http://stackoverflow.com/questions/4307877/uploading-files-with-phonegap-iphone –

2

にこれを直接投稿するのPhoneGap APIを使用しますPhoneGapプラグインを使用してGraph API iPhone/iPadからFacebookに写真を投稿してください。このプラグインは、Facebookの場所ID(Checkin Facebook with photo)を持つ写真を投稿することもでき、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 

第2、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 

注:このメソッドはポストされたときにコールバックされ、コード行がコメントアウトされ、ユーザーに警告が表示されます。

第3に、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(例えばファイル:/// ...)で、メッセージは、ユーザーのコメントやplace_idですが、場所IDですFacebookで(例えば、106522332718569)。

そして、他のプラグインと同じように、私たちはプラグインに値としてキーとPhotoCheckinとしてphotoCheckinを追加し、リソース/ Cordova.plistを編集する必要があります。そして、PhotoCheckin.h & PhotoCheckin.mプラグインという名前のフォルダにを入れてPhotoCheckin.jsあなたのJSコードでファイルが含まれています。

楽しいです!台灣から

+0

それでは、電話機の画像をポストすることができます)?あなたの答えがそれを説明するなら、それは良いでしょう。 –

+0

他のネイティブな目的と同じように、C iOSアプリケーションはグラフAPI([コード例](http://stackoverflow.com/q/11974972/1776862))によってFBにローカル写真を投稿できます。ネイティブコードを使用して写真のアップロードを処理します:) – FUNction