1

I am trying to load a bunch of url which is a direct link to an image. The problem is that some of the images are not there and I want to check if it exists before I attempt to load it. I'm using AFNetworking and the functions that I use seem to load all the images and there's a long delay. Is there a way to just batch check all the urls for a 404 without attempting to download the image?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
ken
  • 87
  • 1
  • 6
  • If image is not there then also URL will come from webservice..? – Vidhyanand Jun 12 '14 at 11:58
  • 1
    possible duplicate of [Finding whether a URL is valid and does it exists?](http://stackoverflow.com/questions/15128237/finding-whether-a-url-is-valid-and-does-it-exists) – Lord Zsolt Jun 12 '14 at 11:59
  • The `HEAD` request method gets you a response with only the header and no body in it. As you just want to check the status, that would be enough I guess. I couldn't tell though if you can issue a `HEAD` request with the tool you're using... but that's definitely worth looking. Note that this will only work quick if the domain is OK. if not, then the request will take longer even if it fails because of the higher DNS lookup timeout. (there must be some latency in case the network is slow...) – Laurent S. Jun 12 '14 at 12:03
  • 1
    possible answer http://stackoverflow.com/questions/2985229/if-url-exists-objective-c – abmussani Jun 12 '14 at 12:06

2 Answers2

0

After looking through the answers here's the answer on setting it with AFNetworking. Works perfectly for me.

    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);
    }];
ken
  • 87
  • 1
  • 6
0

Check wheter the url is passing with spaces.and the trim the space from the url. Use this for your image url stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]

madhu
  • 961
  • 9
  • 21