IOS Get network image size change Image color value what set of methods Gray

Source: Internet
Author: User

directly on behalf of the dock file

Picture processing 0 Half gray  1 grayscale   2 dark brown    3 inverse color + (uiimage*) Imagewithimage: (uiimage*) image grayleveltype: ( Uiimagegrayleveltype) type;//color is dimmed how many 0.0-1.0+ (uiimage*) Imagewithimage: (uiimage*) image darkvalue: (float) darkvalue;/ * * to     get the size of the network picture, first to get the picture by the file header     the failure will download the full picture data to calculate the size so it's best not to put it on the main thread    assuming you have a sdwebimage, you'll look at it first. Sdwebimage has cached the image without    the file header size supported format PNG, GIF, JPG   http://www.cocoachina.com/bbs/read.php?tid=165823 */+ (cgsize) Downloadimagesizewithurl: (ID) ImageURL;


Code implementation:


Hate warning-(ID) Diskimagedatabysearchingallpathsforkey: (ID) key{return nil;}    + (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;    nsstring* absolutestring = url.absolutestring;        #ifdef Dispatch_main_sync_safe if ([[Sdimagecache Sharedimagecache] diskimageexistswithkey:absolutestring]) {        uiimage* image = [[Sdimagecache Sharedimagecache] imagefrommemorycacheforkey:absolutestring]; if (!image) {nsdata* data = [[Sdimagecache Sharedimagecache] Performselector: @selector (Diskimagedatabyse            Archingallpathsforkey:) withObject:URL.absoluteString];        Image = [UIImage imagewithdata:data];        } if (image) {return image.size; }} #endif Nsmutableurlrequest *request = [[NsmutableuRlrequest alloc] Initwithurl:url];        nsstring* pathextendsion = [Url.pathextension lowercasestring];    Cgsize size = Cgsizezero;    if ([Pathextendsion isequaltostring:@ "PNG"]) {size = [self downloadpngimagesizewithrequest:request];    } else if ([pathextendsion isequal:@ "GIF"]) {size = [self downloadgifimagesizewithrequest:request];    } else{size = [self downloadjpgimagesizewithrequest:request]; } if (Cgsizeequaltosize (Cgsizezero, size)) {nsdata* data = [Nsurlconnection sendsynchronousrequest:[nsurlrequ        EST Requestwithurl:url] returningresponse:nil error:nil];        uiimage* image = [UIImage imagewithdata:data]; if (image) {#ifdef Dispatch_main_sync_safe [[Sdimagecache Sharedimagecache] Storeimage:image Recalculatef        Romimage:yes imagedata:data forKey:URL.absoluteString Todisk:yes]; #endif size = image.size; }} 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:&amP;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; }    }}


Download url://http://download.csdn.net/detail/li6185377/7368213



Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.

IOS Get network image size change Image color value what set of methods Gray

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.