2012-12-13 46 views
0

我的應用必須從Facebook下載個人資料圖片。我寫信給下載圖像的代碼工作得很好用具有直接鏈接(例如:http://www.mydomain.com/profile_picture.png)圖像URLAFImageRequestOperation產生錯誤

但對於Facebook的網址(例如:http://graph.facebook.com/100000741043402/picture)產生一個錯誤

以下是代碼:

NSURLRequest *imageRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:imageUrl] cachePolicy:NSURLRequestReturnCacheDataDontLoad timeoutInterval:30.0]; 
    AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:imageRequest 
                       imageProcessingBlock:nil 
                         cacheName:@"nscache" 
                          success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){                                                 profilePicture.image = image; 

                           [profilePicture.layer setBorderColor: [[UIColor colorWithRed:137.0/255.0 green:137.0/255.0 blue:137.0/255.0 alpha:1.0] CGColor]]; 
                           [profilePicture.layer setBorderWidth: 2.0]; 
                          } 
                          failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ NSLog(@"%@",[error description]); } 
              ]; 
    [operation start]; 

而且下面是迴響:

Error Domain=NSURLErrorDomain Code=-1008 "resource unavailable" UserInfo=0x97e0ae0 {NSErrorFailingURLStringKey=http://graph.facebook.com/100000741043402/picture, NSErrorFailingURLKey=http://graph.facebook.com/100000741043402/picture, NSLocalizedDescription=resource unavailable, NSUnderlyingError=0xa3559b0 "resource unavailable"} 

任何提示/建議將高度讚賞。

回答

3

如果您使用curl轉儲整個HTTP會話的標題,你會看到:

$curl -IL http://graph.facebook.com/100000741043402/picture 
HTTP/1.1 302 Found 
Access-Control-Allow-Origin: * 
Cache-Control: private, no-cache, no-store, must-revalidate 
Content-Type: image/jpeg 
Expires: Sat, 01 Jan 2000 00:00:00 GMT 
Location: http://profile.ak.fbcdn.net/hprofile-ak-snc6/186199_100000741043402_1626005174_q.jpg 
Pragma: no-cache 
X-FB-Rev: 693801 
X-FB-Debug: dB4yzLkbQuq46xPMq51wruVSccPCm94U4tePFC/VKoc= 
Date: Thu, 13 Dec 2012 07:00:39 GMT 
Connection: keep-alive 
Content-Length: 0 

HTTP/1.1 200 OK 
Content-Type: image/jpeg 
Content-Length: 3055 
Last-Modified: Fri, 01 Jan 2010 00:00:00 GMT 
X-Backend: hs478.snc6 
X-BlockId: 186199 
X-Object-Type: PHOTO_PROFILE 
Access-Control-Allow-Origin: * 
Cache-Control: max-age=1209600 
Expires: Thu, 27 Dec 2012 07:00:40 GMT 
Date: Thu, 13 Dec 2012 07:00:40 GMT 
Connection: keep-alive 

所以FB被重定向到您的個人資料圖片,但的NSURLRequest自身不進行重定向。 This post (Handling redirects correctly with NSURLConnection)顯示了你如何處理這個問題,主要教訓是你必須遵循重定向,直到你最終得到200個響應,它將指示終端URL。