将图像上传到Facebook

时间:2012-04-05 07:06:35

标签: iphone ios facebook

我将以下用于将UIImage发布到用户的Facebook墙的代码无效。我查看了与facebook sdk一起提供的示例代码及其工作正常。我无法找到我的代码出了什么问题。

-(void) shareOnFacebook
{
facebook = [[Facebook alloc] initWithAppId:@"MYAPPID" andDelegate:(id)self];

NSArray * neededPermissions = [[NSArray alloc] initWithObjects:@"offline_access",@"user_about_me", @"publish_stream", @"user_photos", nil] ;
[facebook authorize:neededPermissions ];

if (![facebook isSessionValid]) 
{





    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   img, @"picture",
                                   nil];
    [facebook requestWithGraphPath:@"me/photos"
                                    andParams:params
                                andHttpMethod:@"POST"
                                  andDelegate:(id)self];



} 

}

请告诉我那可能是什么问题?

提前感谢..

2 个答案:

答案 0 :(得分:2)

你需要在调用委托方法- (void)fbDidLogin之后调用上传函数..尝试这样做,它应该工作

- (void)fbDidLogin {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[fb accessToken] forKey:@"FBAccessTokenKey"];
    [defaults setObject:[fb expirationDate] forKey:@"FBExpirationDateKey"];
    [defaults synchronize];

    [self uploadImageWithName:yourImageName];
}


- (void) uploadImageWithName : (NSString*)name
{
    NSString *img = [NSString stringWithFormat:@"%@.PNG",name];
    UIImage *uploadImage = [UIImage imageNamed:img];

    NSMutableDictionary* params = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                   [fb accessToken], 
                                   @"access_token",nil];

    [params setObject:uploadImage forKey:@"source"];

    [fb requestWithGraphPath:@"me/photos"
                             andParams: params
                         andHttpMethod: @"POST"
                           andDelegate:self];

}

答案 1 :(得分:0)

这没有意义,是吗?您询问facebook对象,如果会话有效,如果不是,则继续上传照片。我不知道代码的其余部分是否正常,但至少尝试删除条件或将其更改为if ([facebook isSessionValid]),看看是否有帮助。