繁体   English   中英

从Craigslist下载图像

[英]Download an image From 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
                       }];

而且在图像视图中我仍然看不到图像。

问题是,如果我更改URL并使用相同的确切代码,则将图像下载得很好。 我尝试将图像加载到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

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

  • 网址是虚假的
  • 图片网址是临时的(即将过期),在您请求之前已消失
  • 图片网址要求您登录(并提供错误的错误代码)
  • 服务器通过检查引荐来源网址来防止将图像加载到特定网页的上下文之外。

我的猜测将是最后一个。 如果是这样,请将NSURLRequest上的“ Referer”(故意拼写错误)标题字段设置为包含相关图像的页面的URL,即可解决此问题,但您可能应该与Craigslist进行核对,以确保这样做不会违反他们的服务条款。 否则,他们很有可能会将您应用的用户代理字符串列入黑名单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM