1

我正在尝试在 Objective-c 中获取 facebook 个人资料图片的 URL。我这样做如下。

-(NSString *)getProfileURL:(NSString *)userId{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    request = [[NSMutableURLRequest alloc] init];

    NSString *requestURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture",userId];
    NSLog(@"request url is %@",requestURL);
    [request setURL:[NSURL URLWithString:requestURL]];
    [request setHTTPMethod:@"GET"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setTimeoutInterval:60.0];

    NSError *error;
    NSURLResponse *response;
    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSLog(@"data received from url: %@", data);

    return data;
}

当我在浏览器中从 requestURL 复制并粘贴日志时,我得到了正确的图像。但是当我查看日志时"Data received from url: "。我总是越来越 (null)

有什么帮助吗?

4

1 回答 1

1

根据Graph API 文档,您调用的 URL 直接返回图像,这就是为什么您可以将返回的值加载到 aNSString中,您可以改为创建一个UIImage实例,如下所示:

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
UIImage *image = [[UIImage alloc]initWithData:urlData];
于 2013-02-18T15:20:45.797 回答