简体   繁体   中英

getting the facebook profile picture with a simple NSMutableURLRequest

I'm trying to get the URL for facebook profile pictures in objective-c. 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. But when I look at the log "Data received from url: " . I always getting (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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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