将照片上传到Facebook页面

时间:2012-06-20 09:53:52

标签: iphone facebook-graph-api

我想在页面墙上发布图片,我知道如何在相册上发布图片,以及用户提供的内容,但是对于页面,它会给出一些问题

我正在使用FBGraph API并希望将照片上传到Facebook页面,您可以通过提供任何教程或任何代码库的链接来指导我。

目前我发布的所有图片都显示在我的墙上,而不是我要发布的页面。

我使用了以下代码

-(void)postPictureToWall
{
    NSMutableDictionary *variables = [[NSMutableDictionary alloc] init];

    //create a FbGraphFile object insance and set the picture we wish to publish on it
    UIImage *frontImageNew=self.imgvgalleryImage.image;

    FbGraphFile *graph_file = [[FbGraphFile alloc] initWithImage:frontImageNew];

    //finally, set the FbGraphFileobject onto our variables dictionary....
    [variables setObject:graph_file forKey:@"file"];

    NSDateFormatter *df = [[NSDateFormatter alloc]init];
    [df setDateStyle:NSDateFormatterMediumStyle];
    [df setTimeStyle:NSDateFormatterMediumStyle];

    NSString *bodyString=[NSString stringWithFormat:@"%@",[df stringFromDate:[NSDate date]]];

    [variables setObject:bodyString forKey:@"message"];

 FbGraphResponse *fb_graph_response =  [objFBGraph doGraphPost:@"pageid_here/feed" withPostVars:variables];
SBJSON *parser = [[SBJSON alloc] init];
NSDictionary *facebook_response = [parser objectWithString:fb_graph_response.htmlResponse error:nil];   
    NSLog(@"fb response = %@",facebook_response);

}

使用上面的代码,它在页面墙上成功发布文本,但不是图像

1 个答案:

答案 0 :(得分:0)

您可以使用ASIHTTPRequest来完成此操作。请尝试将其用于我的代码。

- (IBAction为)shareClicked:(ID)发送方     {

//    if (selectedImage) {
//        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Social Plus"
//                                                       message:@"Please select an image to share"
//                                                      delegate:self
//                                             cancelButtonTitle:@"OK"
//                                             otherButtonTitles:nil];
//        [alert show];
//    }
//    else {
//        

    NSString *filePath = [[NSBundle mainBundle]pathForResource:@"f_selected" ofType:@"png"];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/me/photos"]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    request.delegate = self;
    [request addFile:filePath forKey:@"file"];
    [request setPostValue:@"here will be the caption" forKey:@"message"];
    [request setPostValue:[[NSUserDefaults standardUserDefaults]objectForKey:@"access_token"] forKey:@"access_token"];
    [request setAllowCompressedResponse:NO];
//    [request setTimeOutSeconds:60];

    [request setDidFinishSelector:@selector(uploadRequestFinished:)];
    [request startAsynchronous];
//    }
}

-(void)uploadRequestFinished:(ASIHTTPRequest *)request
{

    NSString *responseStr = [request responseString];
    NSLog(@"%@",responseStr);
}