25

我已经参考以下链接完成了以下代码,以创建在 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";
            }
        );
    }
)

我不知道这些文件是如何附加的。任何人都可以帮我解决这个问题。

我的代码是否有任何更改,请告诉我。

提前致谢...

4

5 回答 5

1

我建议您使用 iOS 6 中新的集成 Facebook API 来执行此操作,而不是 Facebook 的 API:https ://developer.apple.com/videos/wwdc/2012/?id= 306 所有社交任务都有一个黄金 WWDC 视频。另外,在 iOS 6 中使用新的 API 比使用 Facebook 快得多。

于 2012-10-21T06:04:13.680 回答
0

For your NSDictionary of parameters ,use the NSData representation, instead of directly using the UIImage object.

Depending on the compression format of the images you are trying to upload, you'll need to use a different method to convert the UIImage to NSData. Here are two techniques, one for PNG and one for JPEG

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

This is a tutorial from the Facebook Developer blog that shows something similar (although it is for a video, and not the batch API), and one significant difference from your code is that the parameter passed is in NSData representation, not UIImage.

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

于 2012-05-28T21:03:57.090 回答
0

[参数 setObject:UIImagePNGRepresentation(self.image1) forKey:@"file1"];

这不应该是数据,它应该是该图像的路径。如果您提供路径而不是 NSData,它将正常工作。

于 2012-10-31T12:18:25.260 回答
0

我得到了使用 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];

}

谢谢梅胡尔。

于 2013-06-10T12:28:17.370 回答
-1

您错误地将二进制文件添加到 Dictionary 中。看看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];
}
于 2012-05-08T07:47:35.077 回答