IOS--Get the size of local pictures and network pictures

Source: Internet
Author: User

// gets the size        of the picture Cgsize size = [UIImage imagenamed:@ "regstep2_sex"].size;

to get the size of a network picture:

//get picture size based on image URL+ (Cgsize) Getimagesizewithurl: (ID) imageurl{Nsurl* URL =Nil; if([ImageURL Iskindofclass:[nsurlclass]]) {URL=ImageURL; }    if([ImageURL iskindofclass:[nsstringclass]]) {URL=[Nsurl Urlwithstring:imageurl]; }    if(URL = =Nil)returnCgsizezero;//URL returned incorrectly Cgsizezeronsmutableurlrequest*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))//sending an asynchronous request to request the original image if the file header information fails to get{NSData* data =[nsurlconnection sendsynchronousrequest:[nsurlrequest Requestwithurl:url] Returningresponse:nil Error:nil]; UIImage* Image =[UIImage Imagewithdata:data]; if(image) {size=image.size; }    }    returnsize;}//get the size of a PNG picture+ (Cgsize) Getpngimagesizewithrequest: (nsmutableurlrequest*) request{[request SetValue:@"bytes=16-23"Forhttpheaderfield:@"Range"]; NSData* data =[nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil]; if(Data.length = =8)    {        intW1 =0, W2 =0, W3 =0, W4 =0; [Data getBytes:&AMP;W1 Range:nsmakerange (0,1)]; [Data getBytes:&AMP;W2 Range:nsmakerange (1,1)]; [Data getBytes:&AMP;W3 Range:nsmakerange (2,1)]; [Data getBytes:&w4 Range:nsmakerange (3,1)]; intW = (W1 << -) + (W2 << -) + (W3 <<8) +W4; intH1 =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)]; inth = (H1 << -) + (H2 << -) + (H3 <<8) +h4; returnCgsizemake (W, h); }    returnCgsizezero;}//get the size of a 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)    {         ShortW1 =0, W2 =0; [Data getBytes:&AMP;W1 Range:nsmakerange (0,1)]; [Data getBytes:&AMP;W2 Range:nsmakerange (1,1)];  ShortW = W1 + (W2 <<8);  ShortH1 =0, H2 =0; [Data getBytes:&h1 Range:nsmakerange (2,1)]; [Data getBytes:&h2 Range:nsmakerange (3,1)];  ShortH = h1 + (H2 <<8); returnCgsizemake (W, h); }    returnCgsizezero;}//get the size of a 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) {        returnCgsizezero; }         if([Data length] < About) {//there must be only one DQT field         ShortW1 =0, W2 =0; [Data getBytes:&AMP;W1 Range:nsmakerange (0x60,0x1)]; [Data getBytes:&AMP;W2 Range:nsmakerange (0x61,0x1)];  ShortW = (W1 <<8) +W2;  ShortH1 =0, H2 =0; [Data getBytes:&h1 Range:nsmakerange (0x5e,0x1)]; [Data getBytes:&h2 Range:nsmakerange (0x5f,0x1)];  Shorth = (H1 <<8) +H2; returnCgsizemake (W, h); } Else {         ShortWord =0x0; [Data getBytes:&word Range:nsmakerange (0x15,0x1)]; if(Word = =0xdb) {[Data getBytes:&word Range:nsmakerange (0x5a,0x1)]; if(Word = =0xdb) {//two x DQT fields                 ShortW1 =0, W2 =0; [Data getBytes:&AMP;W1 Range:nsmakerange (0xa5,0x1)]; [Data getBytes:&AMP;W2 Range:nsmakerange (0xa6,0x1)];  ShortW = (W1 <<8) +W2;  ShortH1 =0, H2 =0; [Data getBytes:&h1 Range:nsmakerange (0xa3,0x1)]; [Data getBytes:&h2 Range:nsmakerange (0xa4,0x1)];  Shorth = (H1 <<8) +H2; returnCgsizemake (W, h); } Else{//a DQT field                 ShortW1 =0, W2 =0; [Data getBytes:&AMP;W1 Range:nsmakerange (0x60,0x1)]; [Data getBytes:&AMP;W2 Range:nsmakerange (0x61,0x1)];  ShortW = (W1 <<8) +W2;  ShortH1 =0, H2 =0; [Data getBytes:&h1 Range:nsmakerange (0x5e,0x1)]; [Data getBytes:&h2 Range:nsmakerange (0x5f,0x1)];  Shorth = (H1 <<8) +H2; returnCgsizemake (W, h); }        } Else {            returnCgsizezero; }    }}

IOS--Get the size of local pictures and network pictures

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.