2014-10-16 55 views
2

我一直在試圖下載從URL的圖像(Craigslist的更確切)下載圖像從Craigslist的

首先,我已經使用了SDWebImage框架,我不能看到的圖像。從那以後,我試過一個簡單的方法來下載網址:

使用這個網址的圖片:

http://images.craigslist.org/01212_fxFfHsZFhoi_600x450.jpg

UIImageView image; 
NSString *urlstring = [NSString stringWithFormat:@"http://images.craigslist.org/01212_fxFfHsZFhoi_600x450.jpg"]; 
NSURL *url = [NSURL URLWithString:urlstring]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
[image setImageWithURLRequest:request 
       placeholderImage:[UIImage imageNamed:@"placeholder.png"] 
         success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image){ 
          // Succes on loading image 
         } 
         failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){ 
          // Fail to load image 
         }]; 

,我仍然看不到我的圖像查看圖像。

問題是,如果我更改網址並使用相同的確切代碼,圖像將被下載得很好。我試圖在Safari中加載圖像,它工作得很好,所以鏈接沒問題。

例如我用: http://monsieurstolli.files.wordpress.com/2013/10/meh.jpg

和它的作品。

我不知道如何下載該圖像。

編輯:

我做了一點調查,我改變了代碼:

AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlRequest]; 
    requestOperation.responseSerializer = [AFImageResponseSerializer serializer]; 
    requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"]; 
    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Response: %@", responseObject); 
     _imga.image = responseObject; 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Image error: %@", error); 
    }]; 
    [requestOperation start]; 

    } 

,但這個時候,我得到了以下錯誤:

2014-10-16 15:07:10.537 CraigslistChecker[3192:907] Image error: Error Domain=com.alamofire.error.serialization.response Code=-1011 "Request failed: not found (404)" UserInfo=0x1c5b8e30 {com.alamofire.serialization.response.error.response=<NSHTTPURLResponse: 0x1c59ac60>, NSErrorFailingURLKey=images.craigslist.org/00W0W_blCL2sqk1Jt_600x450.jpg, NSLocalizedDescription=Request failed: not found (404), com.alamofire.serialization.response.error.data=<0d0a>} 
purgeIdleCellConnections: found one to purge conn = 0x1c59b1f0 
+0

請注意,「代碼段」按鈕用於嵌入JavaScript - 不適用於一般代碼示例。改爲使用「代碼示例」按鈕(Ctrl-K)。 – Anna 2014-10-16 12:33:26

回答

0

您發佈的URL返回當我在Web瀏覽器中請求它時,出現404(未找到)錯誤,這意味着服務器拒絕向您提供URL。因此,要麼:

  • 的網址是假
  • 圖像URL是臨時的(即將到期)走了,你要求它
  • 圖像URL需要你在(登錄之前並給出錯誤碼)
  • 服務器通過檢查引用來防止圖像被加載到特定網頁的上下文之外。

我的猜測是最後一個。如果是這樣,將NSURLRequest中的「Referer」(拼寫錯誤的意圖)標題字段設置爲包含該圖像的頁面的URL應該可以解決問題,但是您應該檢查Craigslist以確保這樣做不會違反他們的服務條款。否則,他們很可能會將您應用的用戶代理字符串列入黑名單。