我一直在尝试从url (更准确地说是Craigslist)下载一张图片。
首先,我使用了SDWebImage框架,我看不到图像。之后,我尝试了一种简单的方法来下载url:
使用此图像的url:
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中加载图像,它工作得很好,所以链接是ok的。
例如,我使用了: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发布于 2016-12-29 09:36:24
当我在web浏览器中请求您发布的URL时,它会返回404 (未找到)错误,这意味着服务器拒绝向您提供该URL。因此,要么:
我猜是最后一次了。如果是这样的话,将NSURLRequest上的"Referer“(有意拼写错误)标头字段设置为包含有问题的图片的页面的URL应该可以解决这个问题,但您可能应该与Craigslist核实一下,以确保这样做不会违反他们的服务条款。否则,他们很有可能会将你的应用程序的用户代理字符串列入黑名单。
https://stackoverflow.com/questions/26403399
复制相似问题