真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

IOS獲取網(wǎng)絡(luò)圖片大小實例詳解-創(chuàng)新互聯(lián)

IOS 獲取網(wǎng)絡(luò)圖片大小實例詳解

成都網(wǎng)站設(shè)計、做網(wǎng)站介紹好的網(wǎng)站是理念、設(shè)計和技術(shù)的結(jié)合。創(chuàng)新互聯(lián)擁有的網(wǎng)站設(shè)計理念、多方位的設(shè)計風格、經(jīng)驗豐富的設(shè)計團隊。提供PC端+手機端網(wǎng)站建設(shè),用營銷思維進行網(wǎng)站設(shè)計、采用先進技術(shù)開源代碼、注重用戶體驗與SEO基礎(chǔ),將技術(shù)與創(chuàng)意整合到網(wǎng)站之中,以契合客戶的方式做到創(chuàng)意性的視覺化效果。

在iOS開發(fā)過程中經(jīng)常需要通過網(wǎng)絡(luò)請求加載圖片,有時,需要在創(chuàng)建UIImageView或UIButton來顯示圖片之前需要提前知道圖片的尺寸,根據(jù)圖片尺寸創(chuàng)建對應(yīng)大小的控件。但是對于網(wǎng)絡(luò)圖片來說,要想通過最優(yōu)的方法獲得尺寸就略微有點困難,大體思路就是下面這種:

如果有使用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 h2 = 0, h3 = 0, h4 = 0, h5 = 0;

    [data getBytes:&h2 range:NSMakeRange(4, 1)];

    [data getBytes:&h3 range:NSMakeRange(5, 1)];

    [data getBytes:&h4 range:NSMakeRange(6, 1)];

    [data getBytes:&h5 range:NSMakeRange(7, 1)];

    int h = (h2 << 24) + (h3 << 16) + (h4 << 8) + h5;

    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 h2 = 0, h3 = 0;

    [data getBytes:&h2 range:NSMakeRange(2, 1)];

    [data getBytes:&h3 range:NSMakeRange(3, 1)];

    short h = h2 + (h3 << 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 h2 = 0, h3 = 0;

    [data getBytes:&h2 range:NSMakeRange(0x5e, 0x1)];

    [data getBytes:&h3 range:NSMakeRange(0x5f, 0x1)];

    short h = (h2 << 8) + h3;

    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 h2 = 0, h3 = 0;

        [data getBytes:&h2 range:NSMakeRange(0xa3, 0x1)];

        [data getBytes:&h3 range:NSMakeRange(0xa4, 0x1)];

        short h = (h2 << 8) + h3;

        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 h2 = 0, h3 = 0;

        [data getBytes:&h2 range:NSMakeRange(0x5e, 0x1)];

        [data getBytes:&h3 range:NSMakeRange(0x5f, 0x1)];

        short h = (h2 << 8) + h3;

        return CGSizeMake(w, h);

      }

    } else {

      return CGSizeZero;

    }

  }

}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.cdcxhl.com,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


網(wǎng)站題目:IOS獲取網(wǎng)絡(luò)圖片大小實例詳解-創(chuàng)新互聯(lián)
文章路徑:http://weahome.cn/article/dpcohh.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部