簡體   English   中英

使用批量請求在Facebook上上傳多張照片

[英]Upload multiple photos on Facebook using Batch Request

我參考以下鏈接完成了以下代碼,以創建在Facebook上傳多張照片的批量請求。

我有一些解決方案可以通過這個Facebook圖形API在Facebook上傳多張照片

碼:

    NSString *jsonRequest1 = @"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Hello 1\", \"attached_files\": \"file1\" }";
    NSString *jsonRequest2 = @"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Hello 2\", \"attached_files\": \"file2\" }";
    NSString *jsonRequestsArray = [NSString stringWithFormat:@"[ %@, %@ ]", jsonRequest1, jsonRequest2];
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:jsonRequestsArray,@"batch",nil];
    [params setObject:UIImagePNGRepresentation(self.image1) forKey:@"file1"];
    [params setObject:UIImagePNGRepresentation(self.image2) forKey:@"file2"];
    [objFacebook requestWithGraphPath:@"me" andParams:params andHttpMethod:@"POST" andDelegate:self];

現在,當我運行此代碼時,我得到以下輸出。

結果字典在 - (void)request:(FBRequest *)request didLoad:(id)result

(
        {
        body = "{\"error\":0,\"error_description\":\"File file1 has not been attached\"}";
        code = 400;
        headers =         (
                        {
                name = "HTTP/1.1";
                value = "400 Bad Request";
            },
                        {
                name = "Content-Type";
                value = "text/javascript; charset=UTF-8";
            }
        );
    },
        {
        body = "{\"error\":0,\"error_description\":\"File file2 has not been attached\"}";
        code = 400;
        headers =         (
                        {
                name = "HTTP/1.1";
                value = "400 Bad Request";
            },
                        {
                name = "Content-Type";
                value = "text/javascript; charset=UTF-8";
            }
        );
    }
)

我不知道這些文件如何附加..任何人都可以幫我弄清楚這個問題。

我的代碼有任何變化,請告訴我。

提前致謝...

我建議您使用iOS 6中新的集成Facebook API來代替Facebook的API: https//developer.apple.com/videos/wwdc/2012/? id = 306這里有一個適用於所有社交任務的黃金WWDC視頻。 另外,使用iOS 6中的新API比使用Facebook更快。

對於NSDictionary參數,請使用NSData表示,而不是直接使用UIImage對象。

根據您嘗試上傳的圖像的壓縮格式,您需要使用不同的方法將UIImage轉換為NSData。 這里有兩種技術,一種用於PNG,另一種用於JPEG

//...cut...
[params setObject:UIImageJPEGRepresentation(self.jpegImage, 0.8) forKey:@"file1"];
[params setObject:UIImagePNGRepresentation(self.pngImage) forKey:@"file2"];
//...cut...

這是來自Facebook開發人員博客的一個教程,它顯示了類似的內容(雖然它適用於視頻,而不是批處理API),與代碼的一個顯着區別是傳遞的參數是NSData表示,而不是UIImage。

http://developers.facebook.com/blog/post/532/

[params setObject:UIImagePNGRepresentation(self.image1)forKey:@“file1”];

這不應該是數據,它應該是該圖像的路徑。 如果您提供路徑而不是NSData,它將正常工作。

我使用FBgraph獲得了多文件上傳的解決方案。

這是我的代碼。

-(void) fbGraphCallback
{

FbGraphFile *graph_file = [[FbGraphFile alloc]initWithImage:imgView1.image];
FbGraphFile *graph_file1 = [[FbGraphFile alloc]initWithImage:imgView2.image];

NSString *jsonRequest1 = @"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"First Image\", \"attached_files\": \"file1\" }";

NSString *jsonRequest2 = @"{ \"method\": \"POST\", \"relative_url\": \"me/photos\" , \"body\": \"Second Image\", \"attached_files\": \"file2\" }";

NSString *jsonRequestsArray = [NSString stringWithFormat:@"[ %@, %@ ]", jsonRequest1, jsonRequest2];

NSMutableDictionary *params = [NSMutableDictionary     dictionaryWithObjectsAndKeys:jsonRequestsArray,@"batch",nil];

[params setObject:graph_file forKey:@"file1"];
[params setObject:graph_file1 forKey:@"file2"];

[fbgraph doGraphPost:@"" withPostVars:params];

}

謝謝mehul。

您正在將二進制文件錯誤地添加到詞典中。 看看Facebook的示例應用程序中的文件上傳方法。 您應該能夠輕松地將Facebook的示例代碼應用到您的應用程序中。

/** 
 * Upload a photo.
 */
-(IBAction)uploadPhoto:(id)sender 
{
  NSString *path = @"http://www.facebook.com/images/devsite/iphone_connect_btn.jpg";
  NSURL *url = [NSURL URLWithString:path];
  NSData *data = [NSData dataWithContentsOfURL:url];
  UIImage *img  = [[UIImage alloc] initWithData:data];

  NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                 img, @"picture",
                                 nil];
  [_facebook requestWithMethodName:@"photos.upload"
                         andParams:params
                     andHttpMethod:@"POST"
                       andDelegate:self];
  [img release];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM