【发布时间】:2014-10-16 11:36:05
【问题描述】:
我一直在尝试从 url 下载图片(更准确地说是 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
【问题讨论】:
-
请注意,“代码 sn-p”按钮用于嵌入 JavaScript,而不是用于一般代码示例。请改用“代码示例”按钮 (Ctrl-K)。
标签: ios objective-c nsurlrequest