iphone - AFImageRequestOperation 产生错误

标签 iphone ios afnetworking

我的应用程序必须从 facebook 下载个人资料图片。我编写的用于下载图像的代码适用于具有直接链接的图像 url(例如:http://www.mydomain.com/profile_picture.png)

但它会为 facebook url 产生错误(例如: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"}

如有任何提示/建议,我们将不胜感激。

最佳答案

如果您使用 curl 转储整个 HTTP session 的 header ,您将看到:

$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)展示了如何处理这个问题,主要的教训是您必须遵循重定向,直到得到指示终端 URL 的 200 响应为止。

关于iphone - AFImageRequestOperation 产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13854421/

相关文章:

ios - 发布到服务器,将 ASIFormDataRequest 替换为 AFnetworking

ios - 通过 AFNetworking 使用 S3 预签名帖子

iphone - 显示图像的性能问题

ios - 与所有可能的实体有一个共享按钮 IoS

iphone - UITextView 在 'Done' 上退出第一响应者

objective-c - AFNetworking 如何检查上传操作是否完成或失败

iphone - 如果 iWork 文档基于 XML,但 iOS 不支持 XML 持久性,那么 iPad 版 iWork 应用程序又如何呢?

ios - 如何在设备上的每个触摸位置周围绘制一个圆圈?

iOS - 在 View Controller 中绘制一个 donut

ios - 为什么 NSLayoutConstraints 被忽略?