1

Is there any way to get the image dimensions or size without actually downloading it from the server.

Like if a image is hosted on https://500px.com/ or https://imgur.com/ and i want to do some calculations but i also want to save the bandwidth. If the image size is quite large and the user bandwidth is not so good i want to queue the image for later.

Assuming that API doesn't provide such information.

harsh_v
  • 3,193
  • 3
  • 34
  • 53

3 Answers3

1

In the didRecieveResponse delegate method for the NSURLConnection you will recieve a response that contains such info. if and only if those has been set on server:

The following gets you the disk size of the image:

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
    int state = [httpResponse statusCode];

    if (state >= 400 && state <600)
    {
         // something wrong happen
    }

    NSLog(@"Download Response : %@", [response description]);  // this shows all the info. available by server. 

    int file_size = [response expectedContentLength];
}

You can check for the size and then cancel the connection if you want to.

For image dimensions. check this question. the answer claims that it can be done with the suggested fastimage category.

The only logical way to get this done is by downloading the first part of the image that contains the image header. which contains such an info as the image type(png, jpg ...) and the image dimensions. I believe that this library do so.

Community
  • 1
  • 1
hasan
  • 23,815
  • 10
  • 63
  • 101
  • i was able to retrieve the `"Content-Length"` as you suggested but I am little confused as, the *size of the image* on **disk** is **(418KB)** but the result of `"Content-Length"` is 584 in **bytes**. Is it really working? The URL on which i tested was a direct link to image, like if you open it on browser you will have the image downloaded. – harsh_v Aug 16 '15 at 20:19
  • maybe the server is setting that wrong (don't know if that possible). but, maybe the server do some processing on the image adding some signature. is that possible? – hasan Aug 16 '15 at 20:21
  • the only different I faced before when the server was a windows. it calculate the size differently. becuase kb in mac is 1024 b and 1000 in windows. the different shouldn't be that big. – hasan Aug 16 '15 at 20:23
  • The URL to image is a direct link to image, like if you open it on browser you will have the image downloaded. – harsh_v Aug 16 '15 at 20:23
  • which link? the links in the questions does not open images! – hasan Aug 16 '15 at 20:25
  • I meant ,I tested the code on images stored over imgur – harsh_v Aug 16 '15 at 20:27
  • the following suggest that the image could be compressed. and that length is the compressed size. when you download you get the uncompressed image: http://stackoverflow.com/questions/14775628/why-does-my-nsurlconnection-report-an-incorrect-expectedcontentlength – hasan Aug 16 '15 at 20:31
1

I don't know if it's possible or not. But here is my tip. you could check the size of the file. see here: How to know the size of a file before downloading it?

and if the size is larger than x you queue the image for later ;-)

Code from linked answer:

URL url = new URL("http://server.com/file.mp3");
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
int file_size = urlConnection.getContentLength();
Community
  • 1
  • 1
Migo
  • 206
  • 2
  • 4
  • No worries. This is an automated response comming from reviewing posts by new users, Alot of links go dead, meaning your answer would be useless in that case, it's just a precaution :) – Marko Aug 16 '15 at 20:58
-2

It's not possible to calculate image size without downloading.

Hari Ram
  • 3,098
  • 5
  • 23
  • 30