如何发布照片.iOS Facebook SDK 3.1

Fed*_*olo 10 http-post facebook-graph-api facebook-ios-sdk

我需要在墙上发布一张图片.照片是在我的iPad应用程序中生成的.

Fed*_*olo 24

这是我发现的最简单的方法

- (void) postImageToFB:(UIImage*)image
{
    NSData* imageData = UIImageJPEGRepresentation(image, 90);    
    NSMutableDictionary * params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    @"This is my drawing!", @"message",
                                    imageData, @"source",
                                    nil];

    [FBRequestConnection startWithGraphPath:@"me/photos"
                                 parameters:params
                                 HTTPMethod:@"POST"
                          completionHandler:^(FBRequestConnection *connection, id result, NSError *error) {

                          }];
}
Run Code Online (Sandbox Code Playgroud)

如果你想发布一个朋友的墙,改变@"me/photos"@"[friendID]/photos"

然后,请求发布和调用该方法的权限

if ([FBSession.activeSession.permissions indexOfObject:@"publish_stream"] == NSNotFound)
{
    // No permissions found in session, ask for it
    [FBSession.activeSession reauthorizeWithPublishPermissions:[NSArray arrayWithObject:@"publish_stream"]
                                               defaultAudience:FBSessionDefaultAudienceFriends
                                             completionHandler:^(FBSession *session, NSError *error)
     {
         // If permissions granted, publish the story
         if (!error) [self postImageToFB:currentDrawing];
     }];
}
// If permissions present, publish the story
else [self postImageToFB:currentDrawing];
Run Code Online (Sandbox Code Playgroud)

如果不存在,将创建"[App Name] Photos"相册

它对我有用!

  • 不需要NSData,只需将其保存为UIImage并将图像键更改为"图片",图形API将处理其余部分:-) (4认同)