我试图在objective-c中获取facebook个人资料图片的网址。我这样做了。
-(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)
。
任何帮助?
答案 0 :(得分:1)
相应于Graph API documentation,您正在调用的URL直接返回图像,这就是为什么您可以将返回的值加载到NSString
,您可以改为创建UIImage
实例,像这样:
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
UIImage *image = [[UIImage alloc]initWithData:urlData];