如何检查URL上是否存在图像

时间:2014-06-12 11:57:19

标签: ios objective-c

我正在尝试加载一堆url,它是图像的直接链接。问题是有些图像不存在,我想在尝试加载它之前检查它是否存在。我正在使用AFNetworking,我使用的功能似乎加载了所有图像,并且有很长的延迟。有没有办法只是批量检查404的所有网址而不试图下载图像?

2 个答案:

答案 0 :(得分:0)

在查看答案后,在AFNetworking上设置答案。适合我。

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:obj[@"product_thumb"][0][@"image_url"]]];
    [request setHTTPMethod:@"HEAD"];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        //NSLog(@"Response: %@", responseObject);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //NSLog(@"Image error: %@", error);
    }];

答案 1 :(得分:0)

检查网址是否与spaces一起传递,并从网址中修剪空间。 用于图像的URL url stringByTrimmingCharactersInSet:[NSCharacterSet whitespace字符集]

相关问题