IOS-Get the size of local pictures and network pictures

Source: Internet
Author: User

Gets the size of the picture        cgsize size = [UIImage imagenamed:@ "20170609.png"].size;

to get the size of a network picture:

Get picture size + (cgsize) Getimagesizewithurl: (id) imageurl{nsurl* url = nil According to the picture URL;    if ([ImageURL Iskindofclass:[nsurl class]]) {URL = ImageURL;    } if ([ImageURL iskindofclass:[nsstring class]]) {URL = [nsurl Urlwithstring:imageurl];                  } if (URL = = nil) return Cgsizezero;    The URL returned incorrectly cgsizezero nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] initwithurl:url];         nsstring* pathextendsion = [Url.pathextension lowercasestring];    Cgsize size = Cgsizezero;    if ([Pathextendsion isequaltostring:@ "PNG"]) {size = [self getpngimagesizewithrequest:request];    } else if ([pathextendsion isequal:@ "GIF"]) {size = [self getgifimagesizewithrequest:request];    } else{size = [self getjpgimagesizewithrequest:request]; if (Cgsizeequaltosize (Cgsizezero, size))//If getting header information fails, send an asynchronous request request original {nsdata* data = [Nsur Lconnection sendsynchronousrequest:[nsurlrequest RequestwithuRl:url] Returningresponse:nil Error:nil];        uiimage* image = [UIImage imagewithdata:data];        if (image) {size = Image.size; }} return size;} Get PNG picture size + (cgsize) Getpngimagesizewithrequest: (nsmutableurlrequest*) request{[Request setvalue:@ "bytes=16-23"    forhttpheaderfield:@ "Range"];    nsdata* data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];        if (data.length = = 8) {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); } return Cgsizezero;} Get the size of the GIF picture + (cgsize) Getgifimagesizewithrequest: (nsmutableurlrequest*) request{[Request setvalue:@ "bytes=6-9"    forhttpheaderfield:@ "Range"];    nsdata* data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];        if (data.length = = 4) {Short W1 = 0, w2 = 0;        [Data Getbytes:&w1 range:nsmakerange (0, 1)];        [Data Getbytes:&w2 Range:nsmakerange (1, 1)];        Short W = W1 + (W2 << 8);        Short H1 = 0, h2 = 0;        [Data Getbytes:&h1 Range:nsmakerange (2, 1)];        [Data Getbytes:&h2 Range:nsmakerange (3, 1)];        Short H = h1 + (H2 << 8);    Return Cgsizemake (W, h); } return Cgsizezero;} Gets the size of the JPG picture + (cgsize) Getjpgimagesizewithrequest: (nsmutableurlrequest*) request{[Request setvalue:@] bytes=0-209 "forhttpheaderfield:@" Range "];         nsdata* data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];    if ([Data length] <= 0x58) {return cgsizezero;        } if ([Data length] < 210) {//must have only one DQT field short w1 = 0, w2 = 0;        [Data getbytes:&w1 Range:nsmakerange (0x60, 0x1)];        [Data getbytes:&w2 Range:nsmakerange (0x61, 0x1)];        Short w = (W1 << 8) + w2;        Short H1 = 0, h2 = 0;        [Data getbytes:&h1 Range:nsmakerange (0x5e, 0x1)];        [Data Getbytes:&h2 Range:nsmakerange (0x5f, 0x1)];        Short h = (H1 << 8) + H2;    Return Cgsizemake (W, h);        } else {Short word = 0x0;        [Data Getbytes:&word Range:nsmakerange (0x15, 0x1)];            if (Word = = 0xdb) {[Data Getbytes:&word range:nsmakerange (0x5a, 0x1)];                if (Word = = 0xdb) {//two DQT fields short w1 = 0, w2 = 0; [Data Getbytes:&w1 Range:nsmakeraNge (0xa5, 0x1)];                [Data getbytes:&w2 Range:nsmakerange (0XA6, 0x1)];                Short w = (W1 << 8) + w2;                Short H1 = 0, h2 = 0;                [Data getbytes:&h1 Range:nsmakerange (0XA3, 0x1)];                [Data Getbytes:&h2 Range:nsmakerange (0XA4, 0x1)];                Short h = (H1 << 8) + H2;            Return Cgsizemake (W, h);                } else {//a DQT field short w1 = 0, w2 = 0;                [Data getbytes:&w1 Range:nsmakerange (0x60, 0x1)];                [Data getbytes:&w2 Range:nsmakerange (0x61, 0x1)];                Short w = (W1 << 8) + w2;                Short H1 = 0, h2 = 0;                [Data getbytes:&h1 Range:nsmakerange (0x5e, 0x1)];                [Data Getbytes:&h2 Range:nsmakerange (0x5f, 0x1)];                Short h = (H1 << 8) + H2;            Return Cgsizemake (W, h);        }} else {return cgsizezero; }    }}

IOS-Get the size of your local picture and network 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.