0

我正在构建一个用于检索用户个人资料图片的功能。我正在使用 facebook 的 FBSDKGraphRequest 但是一旦我尝试使用检索到的数据将其分配到图片中,就会收到一个我不明白的错误:Variable is not assignable (missing _block type specifier)

我的功能如下:

- (UIImage *)getProfilePicture: (NSString *)ID
{

    FBSDKGraphRequest *requestFriends = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me/friends" parameters:@{@"fields": @"name"}];
    FBSDKGraphRequestConnection *connection = [[FBSDKGraphRequestConnection alloc] init];

    //[connection addRequest:requestFriends completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)

    UIImage* profilePic;


    NSString* requestPath = @"me/?fields=name,location,gender,birthday,relationship_status,picture,email,id";
    FBSDKGraphRequest *request = [[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"picture"}];
    [connection addRequest:request completionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError* error)
    {
        NSDictionary* data = (NSDictionary *)result;
        NSDictionary* picture = [data objectForKey:@"picture"];
        NSDictionary* pictureData = [picture objectForKey:@"data"];
        NSString* pictureURL = [pictureData objectForKey:@"url"];
        profilePic = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:pictureURL]]];
    }];

    return profilePic;
}
4

1 回答 1

2

该错误告诉您在声明__block之前添加。profilePic

__block UIImage* profilePic;

然而,它不会解决你的问题。

关键是您的 -(UIImage *)getProfilePicture: (NSString *)ID 方法是同步的,而FBSDKGraphRequest调用(带有完成块)是异步的。

我可能建议您做的是向您的方法添加 UIImage 回调,或者在 Facebook SDK 调用完成块时使用委托模式返回图像。

另一种解决方案,但不是很实用:使调用完全同步,您的方法将等到FBSDKGraphRequest完成然后返回 UIImage,但它会阻塞该方法正在运行的线程(可能是主线程)。

于 2015-12-26T22:06:12.093 回答