IOS Facebook SDK 3上传带有消息的图片

时间:2012-09-26 17:47:12

标签: ios facebook-graph-api

我使用以下代码将图片上传到Facebook墙:

UIImage *img = [UIImage imageNamed:@"logoits.png"];

[FBRequestConnection startForUploadPhoto:img
                       completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

    NSLog(@"Facebook posting %@",result);
    NSLog(@"Facebook posting %@",error);

}];  

它按预期工作但我想为此上传的照片添加消息或标题,并查看FBRequestConnection的文档,没有这样的示例。 http://developers.facebook.com/docs/sdk-reference/iossdk/3.0/class/FBRequestConnection/

如何上传带有消息的图片?

3 个答案:

答案 0 :(得分:45)

在Facebook SDK 3.0中,只有图像可以使用给定的方法发布,而对于其他操作,您需要使用图形pi。

因此,要发布带有消息的图像,您需要使用graph-api。以下是一行代码,可让您在用户时间轴上发布带有消息的图像。

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:@"your custom message" forKey:@"message"];
[params setObject:UIImagePNGRepresentation(_image) forKey:@"picture"];
_shareToFbBtn.enabled = NO; //for not allowing multiple hits

[FBRequestConnection startWithGraphPath:@"me/photos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection,
                                              id result,
                                              NSError *error) 
 {
     if (error) 
     {
         //showing an alert for failure
         [self alertWithTitle:@"Facebook" message:@"Unable to share the photo please try later."];
     } 
     else
     {
         //showing an alert for success
         [UIUtils alertWithTitle:@"Facebook" message:@"Shared the photo successfully"];
     }
     _shareToFbBtn.enabled = YES;
 }];

并确保仅在登录成功后启用_shareToFbBtn

答案 1 :(得分:5)

linevishy的解决方案效果很好,值得注意的是,如果您使用的是Facebook SDK 3.1并使用了方法

FBRequest *request = [FBRequest requestForUploadPhoto:_imageToShare];

您可以使用以下行代替它:

NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
[params setObject:_textToShare forKey:@"message"];
[params setObject:_imageToShare forKey:@"picture"];
FBRequest *request = [FBRequest requestWithGraphPath:@"me/photos" parameters:params HTTPMethod:@"POST"];

希望它有所帮助!快乐的编码!

答案 2 :(得分:0)