欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

IOS 获取网络图片大小实例详解

程序员文章站 2023-12-19 18:12:34
ios 获取网络图片大小实例详解  在ios开发过程中经常需要通过网络请求加载图片,有时,需要在创建uiimageview或uibutton来显示图片之前需要提前知道图片...

ios 获取网络图片大小实例详解

 在ios开发过程中经常需要通过网络请求加载图片,有时,需要在创建uiimageview或uibutton来显示图片之前需要提前知道图片的尺寸,根据图片尺寸创建对应大小的控件。但是对于网络图片来说,要想通过最优的方法获得尺寸就略微有点困难,大体思路就是下面这种:

如果有使用sdwebimage,则首先检查是否缓存过该图片,如果没有,先通过文件头获取图片大小(针对格式为png、gif、jpg文件获取其尺寸大小),如果获取失败,则下载完整的图片data,然后计算大小,如果有使用sdwebimage,则使用sdwebimage缓存该图片。

实例代码:

+(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)

    {

      //如果未使用sdwebimage,则忽略;缓存该图片

#ifdef dispatch_main_sync_safe

      [[sdimagecache sharedimagecache] storeimage:image recalculatefromimage:yes imagedata:data forkey:url.absolutestring todisk:yes];

#endif

      size = image.size;

    }

  }

  //过滤掉不符合大小的图片,大图太大浪费流量,用户体验不好

  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 << 24) + (w2 << 16) + (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 << 24) + (h2 << 16) + (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) {// 肯定只有一个dqt字段

    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) {// 两个dqt字段

        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 {// 一个dqt字段

        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;

    }

  }

}

 感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: