在iPhone中将图像发布到Facebook

时间:2013-06-18 08:00:28

标签: iphone facebook

您好我正在使用这段代码将图片发布到Facebook

UIImage *image_LocalSharePhoto = [[UIImage alloc]initWithData:sharedSingleton.dataResultImage];
NSDictionary* attachment = [NSDictionary dictionaryWithObjectsAndKeys:
                            @"Put Captions On Pics", @"name",
                            @"Put Captions On Pics", @"caption",
                            image_LocalSharePhoto,@"picture",
                            @"I created this fun photo using a FREE app http://bit.ly/CapsPics. #PutCapsOnPics via @PutCapsOnPics", @"description",
                            nil];

NSString *attachmentStr = [jsonWriter stringWithObject:attachment];

NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                               @"532872463436844", @"api_key",
                               @"Share on Facebook",  @"user_message_prompt",
                               attachmentStr, @"attachment",
                               nil];

[facebook dialog: @"feed" andParams:params andDelegate:self];

它打开了Facebook页面,但没有显示任何要分享或发布的内容。有人可以提供帮助。谢谢!

2 个答案:

答案 0 :(得分:2)

如果您正在使用facebook的api,我可以使用以下代码在用户的时间线上分享图像...请查看代码是否可以帮助.. 包括

// aryFetchedImages = Array of images which needs to be posted on Facebook
NSMutableDictionary* params = [[NSMutableDictionary alloc] init];
for (int i = 0; i < aryFetchedImages.count ; i++)
{
        [params setObject:UIImageJPEGRepresentation([aryFetchedImages objectAtIndex:i], 50.0f)  forKey:@"picture"];
        [FBRequestConnection startWithGraphPath:@"me/photos"
                                     parameters:params
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection,
                                                  id result,
                                                  NSError *error)
         {
             if (error)
             {
                 //showing an alert for failure
                #ifdef DEBUG
                    NSLog(@"Unable to share the photo please try later - Fail %@",error);
                #endif
             }
             else
             {
                 //showing an alert for success
                #ifdef DEBUG
                    NSLog(@"Photo %@ uploaded on Facebook Successfully",UIImageJPEGRepresentation([aryFetchedImages objectAtIndex:i], 50.0f));
                #endif

             }
         }];
}

答案 1 :(得分:1)

如果你使用旧的Facebook方法,你可以发布像下面的方法: -

NSArray* permissions = [NSArray arrayWithObjects:@"publish_stream", @"user_photos", nil];
    [facebook authorize:permissions];

现在发布图像在您的按钮单击或您的逻辑: -

 -(IBAction)ActionPostWallPhoto
    {

         NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                               ImageView.image, @"picture",
                                               nil];

        [[objAppdele facebook] requestWithGraphPath:@"me/feed" andParams:params andHttpMethod:@"POST" andDelegate:self];

    }

如果您使用新的Facebook API,则可以使用以下方法: -

 NSMutableDictionary *postTest = [[NSMutableDictionary alloc]init];
        [postTest setObject:[NSString stringWithFormat@"Your message"] forKey:@"message"];
        [postTest setObject:@"" forKey:@"picture"];
        [postTest setObject:@"" forKey:@"name"];
        [postTest setObject:@"APPID" forKey:@"app_id"];
        NSString *userFBID =[NSString stringWithFormat:@"%@",[allInfoDict objectForKey:@"Friends facebook ID "]];

        [FBRequestConnection startWithGraphPath:[NSString stringWithFormat:@"%@/feed?access_token=%@",userFBID,[appDelegate.mysession accessToken]]
                                     parameters:postTest
                                     HTTPMethod:@"POST"
                              completionHandler:^(FBRequestConnection *connection,
                                                  NSDictionary * result,
                                                  NSError *error) {
                                  if (error) {

                                      NSLog(@"Error: %@", [error localizedDescription]);

                                  } else {
                                      // Success

                                  }
                              }];

        [postTest release];