简体   繁体   English

使用简单的NSMutableURLRequest获取Facebook个人资料图片

[英]getting the facebook profile picture with a simple NSMutableURLRequest

I'm trying to get the URL for facebook profile pictures in objective-c. 我正在尝试在Objective-C中获取Facebook个人资料图片的URL。 I am doing it as follow. 我正在做如下。

-(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;
}

When I copy and past the log from requestURL in my browser, I got the right image. 当我在浏览器中从requestURL复制并粘贴日志时,我得到了正确的图像。 But when I look at the log "Data received from url: " . 但是当我查看"Data received from url: "日志时。 I always getting (null) . 我总是(null)

Any help? 有什么帮助吗?

accordingly to the Graph API documentation , the URL that you are calling returns the image directly, thats why you can load the returned value into a NSString , you can instead create an UIImage instance, like this: 根据Graph API文档 ,所调用的URL直接返回图像,这就是为什么您可以将返回的值加载到NSString ,而可以创建一个UIImage实例,如下所示:

NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
UIImage *image = [[UIImage alloc]initWithData:urlData];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM