iOS:UIImage不可分配(FBSDKGraphRequest)

时间:2015-12-26 22:01:14

标签: ios objective-c facebook uiimage fbsdk

我正在构建一个用于检索用户个人资料图片的功能。我正在使用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;
}

1 个答案:

答案 0 :(得分:2)

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

__block UIImage* profilePic;

然而,它无法解决您的问题。

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

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

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