首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从Craigslist下载图片

从Craigslist下载图片
EN

Stack Overflow用户
提问于 2014-10-16 19:36:05
回答 1查看 366关注 0票数 2

我一直在尝试从url (更准确地说是Craigslist)下载一张图片。

首先,我使用了SDWebImage框架,我看不到图像。之后,我尝试了一种简单的方法来下载url:

使用此图像的url:

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

代码语言:javascript
复制
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
                       }];

我仍然不能在我的图像视图中看到图像。

问题是,如果我更改url并使用相同的代码,图像将被很好地下载。我尝试在Safari中加载图像,它工作得很好,所以链接是ok的。

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

而且它是有效的。

我不知道如何下载那张图片。

编辑:

我做了更多的调查,并修改了代码:

代码语言:javascript
复制
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];

    }

但这一次我得到了以下错误:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2016-12-29 09:36:24

当我在web浏览器中请求您发布的URL时,它会返回404 (未找到)错误,这意味着服务器拒绝向您提供该URL。因此,要么:

  • URL is bogus
  • 图像URL是临时的(即将过期),在您请求它之前就消失了
  • 图像URL要求您登录(并给出错误的错误代码)
  • 服务器通过检查引用人来阻止在特定网页上下文之外加载图像。

我猜是最后一次了。如果是这样的话,将NSURLRequest上的"Referer“(有意拼写错误)标头字段设置为包含有问题的图片的页面的URL应该可以解决这个问题,但您可能应该与Craigslist核实一下,以确保这样做不会违反他们的服务条款。否则,他们很有可能会将你的应用程序的用户代理字符串列入黑名单。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26403399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档