[英]getting the facebook profile picture with a simple NSMutableURLRequest
我正在嘗試在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)
。
有什么幫助嗎?
根據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.