Failed attempt to get network picture size before iOS download picture

Source: Internet
Author: User

I've been looking for a way to get the size of a network image, and finally came up with two conclusions:

1 Use a sub-thread to download it down and then get the size of the picture

2 Use the network to request a picture of the header file, and then find the image size information from the file

The first method requires the main thread to switch between threads, because when using TableView to Reloaddata, the brush table is always a problem, let alone

And then I'm going to focus on a second approach:

Found a blog on the Internet: http://cxjwin.github.io/2013/11/05/preview_image_size/

The great God wrote it very well.

The main idea is to find a picture in the Web first file format, and then against the request back to the data to find, to find the height of the image of the place, parse out

For example, in PNG format, put a piece of code first:

        Case Pngimage:        {            NSString *urlstring = @ "Http://pic12.nipic.com/20110118/1295091_171039317000_2.png";            Nsmutableurlrequest *request=[nsmutableurlrequest Requestwithurl:[nsurl urlwithstring:urlstring] CachePolicy: Nsurlrequestreloadignoringcachedata timeoutinterval:2.0f];            NSString *range=[nsstring stringwithformat:@ "bytes=16-23"];            [Request Setvalue:range forhttpheaderfield:@ "range"];            Nsurlresponse *response=nil;            NSData *data=[nsurlconnection sendsynchronousrequest:request returningresponse:&response Error:NULL];                        Cgsize size = pngimagesizewithheaderdata (data);            NSLog (@ "PNG image size:%@", nsstringfromcgsize (size));        }

or something like that.

    NSString *urlstring = @ "Http://pic12.nipic.com/20110118/1295091_171039317000_2.png";    Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:[nsurl urlwithstring:urlstring]];    [Request Setvalue:kpngrangevalue forhttpheaderfield:@ "Range"];    [[Nsurlconnection Connectionwithrequest:request delegate:self] start];

The way the request is different, the principle is the same

And then we parse the data that was obtained.

Cgsize Pngimagesizewithheaderdata (NSData *data) {        int w1 = 0, w2 = 0, W3 = 0, W4 = 0;    [Data Getbytes:&w1 range:nsmakerange (0, 1)];    [Data Getbytes:&w2 Range:nsmakerange (1, 1)];    [Data getbytes:&w3 Range:nsmakerange (2, 1)];    [Data Getbytes:&w4 Range:nsmakerange (3, 1)];    int w = (W1 << +) + (W2 << +) + (W3 << 8) + W4;        int h1 = 0, h2 = 0, h3 = 0, h4 = 0;    [Data Getbytes:&h1 Range:nsmakerange (4, 1)];    [Data Getbytes:&h2 Range:nsmakerange (5, 1)];    [Data Getbytes:&h3 Range:nsmakerange (6, 1)];    [Data Getbytes:&h4 Range:nsmakerange (7, 1)];    int h = (H1 << +) + (H2 << +) + (H3 << 8) + h4;        Return Cgsizemake (W, h);}

In my experiment, the data obtained is <00000400 000002ad>, only eight bytes, the first four bits are width, and the last four bits are the height

This is the format that he has seen in PNG, so it takes only 16-23 bits to get the data

Data is displayed in 16 binary number, in fact, it should be <00 xx xx ad>, each two number represents 8 bits, binary 8 bits a byte, here to convert, four number can represent a byte

Once this is clear, you can parse these 16 binary numbers according to the header file parsing on the Internet.

Below is a partial parsing of the JPEG header file on the Web

These are normal, and I have a record of the data I get.

FF D8 (SOI) FF E0 (APPO) 00 10 (data length) 4a 46 49 46 00 (identifier) 01 02 (Version number) 01 (density unit) (x density) (y density) 00 (number of thumbnail horizontal pixels) 00 (thumbnail vertical number of pixels) ffed002c 50686f74

And then the rest of it was wonderful.

And then I found the various APPN tags in the data I intercepted, and then this thing didn't know how long, how much, and then the tag that kept the size of the picture.

Ffc0 was buried in the ocean of data, and I found it using search, but it's gone.

A moment, the heart is broken

can get png,gif format picture size, because PNG and GIF format Picture header file fixed, can accurately find the width of the byte, only need very little traffic to get the picture size
But the JPEG header file is very confusing, I really want to say that it does not divide the header and body, the drawing software edits once saved to add a tag code, and then store the height of the tag is lost somewhere in the data flow, because do not know how many drawing software edited, Also do not know the graphics software tag inside the content format, so manually get the JPEG image of the width of the basic impossible

I've been playing so many days.

Or to tinker with the sub-threading it, or to do some webview, it is more reliable than this ah,,,

How can jpeg be so pit

Failed attempt to get network picture size before iOS download picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.