iOS Get network picture size

Source: Internet
Author: User

In iOS development, you often need to load pictures over a network request, and sometimes you need to know the dimensions of the picture before creating a Uiimageview or UIButton to display the picture, creating a corresponding size control based on the size of the picture. But for the network picture, it is slightly difficult to get the size by the best way, the general idea is the following:

If you use Sdwebimage, first check whether the picture has been cached, if not, first get the picture size by the file header (for PNG, GIF, JPG files to get its size), if the acquisition fails, download the full picture data, and then calculate the size, If you are using Sdwebimage, use Sdwebimage to cache the picture.

+ (Cgsize) Downloadimagesizewithurl: (ID) ImageURL

{

nsurl* URL = nil;

if ([ImageURL Iskindofclass:[nsurl class]]) {

URL = ImageURL;

}

if ([ImageURL iskindofclass:[nsstring class]]) {

URL = [Nsurl Urlwithstring:imageurl];

}

if (URL = = nil)

return Cgsizezero;

#ifdef Dispatch_main_sync_safe

if ([[[Sdimagecache Sharedimagecache] diskimageexistswithkey:absolutestring])

{

uiimage* image = [[Sdimagecache Sharedimagecache] imagefrommemorycacheforkey:absolutestring];

if (!image)

{

nsdata* data = [[Sdimagecache Sharedimagecache] Performselector: @selector (diskimagedatabysearchingallpathsforkey:) WithObject:URL.absoluteString];

Image = [UIImage imagewithdata:data];

}

if (image)

{

return image.size;

}

}

#endif

Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc]initwithurl:url CachePolicy: Nsurlrequestuseprotocolcachepolicy Timeoutinterval:1];

nsstring* pathextendsion = [Url.pathextension lowercasestring];

Cgsize size = Cgsizezero;

if ([Pathextendsion rangeofstring:@ "png"].location! = Nsnotfound) {

size = [self downloadpngimagesizewithrequest:request];

}

else if ([pathextendsion rangeofstring:@ "gif"].location! = Nsnotfound)

{

size = [self downloadgifimagesizewithrequest:request];

}

else{

size = [self downloadjpgimagesizewithrequest:request];

}

if (Cgsizeequaltosize (Cgsizezero, size))

{

nsdata* data = [NSData Datawithcontentsofurl:url];

uiimage* image = [UIImage imagewithdata:data];

if (image)

{

If Sdwebimage is not used, the picture is cached

#ifdef Dispatch_main_sync_safe

[[Sdimagecache Sharedimagecache] storeimage:image recalculatefromimage:yes imagedata:data forKey:URL.absoluteString Todisk:yes];

#endif

size = Image.size;

}

}

Filter out the size of the image, big picture is too big to waste traffic, the user experience is not good

if (Size.Height > 2048 | | size.height <= 0 | | size.width > 2048 | | size.width <= 0) {

return Cgsizezero;

}

Else

{

return size;

}

}

+ (Cgsize) Downloadpngimagesizewithrequest: (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;

+ (Cgsize) Downloadgifimagesizewithrequest: (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;

+ (Cgsize) Downloadjpgimagesizewithrequest: (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 network picture size

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.