Detects sdwebimage has no cached pictures IOS get network picture size

Source: Internet
Author: User

nsurl *url = [nsurl urlwithstring:[model.content objectforkey:@ "Image"]];             //synchronous method of requesting network address data              //because this method executes in a child thread (global queue), there is no need to consider the problem of a dead thread              SDWebImageManager *manager = [SDWebImageManager  sharedmanager];             [manager  diskimageexistsforurl:url];            if  ([ Manager diskimageexistsforurl:url])  {                 log (@ "11111")                  self.speakImage = [[manager imageCache]  ImageFromDiskCacheForKey:url.absoluteString];            }else{                 log (@ "3333333")                  nsdata *data = [ nsdata datawithcontentsofurl:url];                 self.speakImage = [UIImage imageWithData:data];             }


IOS Get network picture size

/**
Get the size of the network picture, first to get the picture by the file header
If the failure will download the full picture data to calculate the size, it is best not to put it on the main thread
If you have to use Sdwebimage will first look at the sdwebimage have cached changed pictures no
Formats that support file header size PNG, GIF, JPG http://www.cocoachina.com/bbs/read.php?tid=165823
*/
+ (Cgsize) Downloadimagesizewithurl: (ID) ImageURL;

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]) &NBSP;&NBSP;&NBSP;&NBSP;{&NBSP;&NBSp;      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&NBSP;IMAGE.SIZE;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBSP;&NBSP;&NBSP;&NBSP, #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:[nsurlrequest requestwithurl:url]  returningResponse:nil error:nil];        UIImage*  Image = [uiimage imagewithdata:data];        if (image)         {#ifdef  dispatch_main_sync_safe             [[sdimagecache sharedimagecache] storeimage:image  recalculateFromImage: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&NBSP;==&NBSP;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 << 24)  +  (w2 << 16)  +  (W3 &NBSP;&LT;&LT;&NBSP;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 << 24)  +  (h2  &LT;&LT;&NBSP;16)  +  (h3 << 8)  + h4;         return cgsizemake (w,&NBSP;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&nbsP;= 0;        [data getbytes:&h1; range:nsmakerange (2, 1)];         [data getbytes:&h2; range:nsmakerange (3, 1) ];        short h = h1 +  (h2 << &NBSP;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] &NBSP;&LT;&NBSP;210)  {//  must have only one DQT field &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SHORT&NBSP;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&NBSP;==&NBSP;0XDB)  {            [data  getbytes:&word; range:nsmakerange (0x5a, 0x1)];             if  (WORD&NBSP;==&NBSP;0XDB)  {//  two x DQT fields                  short w1 = 0, w2  = 0;                 [data getbytes:&w1;&nBsp;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)  +&nbsP;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;        }    }}


This article is from "Xiao Liu _blog" blog, please be sure to keep this source http://liuyafang.blog.51cto.com/8837978/1613458

Detects sdwebimage has no cached pictures 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.