查看: 563|回复: 0

[IOS开发教程] IOS 获取网络图片大小实例详解

发表于 2017-8-6 11:35:31

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

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

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

实例代码:

  1. +(CGSize)downloadImageSizeWithURL:(id)imageURL
  2. {
  3. NSURL* URL = nil;
  4. if([imageURL isKindOfClass:[NSURL class]]){
  5. URL = imageURL;
  6. }
  7. if([imageURL isKindOfClass:[NSString class]]){
  8. URL = [NSURL URLWithString:imageURL];
  9. }
  10. if(URL == nil)
  11. return CGSizeZero;
  12. #ifdef dispatch_main_sync_safe
  13. if([[SDImageCache sharedImageCache] diskImageExistsWithKey:absoluteString])
  14. {
  15. UIImage* image = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:absoluteString];
  16. if(!image)
  17. {
  18. NSData* data = [[SDImageCache sharedImageCache] performSelector:@selector(diskImageDataBySearchingAllPathsForKey:) withObject:URL.absoluteString];
  19. image = [UIImage imageWithData:data];
  20. }
  21. if(image)
  22. {
  23. return image.size;
  24. }
  25. }
  26. #endif
  27. NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:1];
  28. NSString* pathExtendsion = [URL.pathExtension lowercaseString];
  29. CGSize size = CGSizeZero;
  30. if ([pathExtendsion rangeOfString:@"png"].location != NSNotFound) {
  31. size = [self downloadPNGImageSizeWithRequest:request];
  32. }
  33. else if([pathExtendsion rangeOfString:@"gif"].location != NSNotFound)
  34. {
  35. size = [self downloadGIFImageSizeWithRequest:request];
  36. }
  37. else{
  38. size = [self downloadJPGImageSizeWithRequest:request];
  39. }
  40. if(CGSizeEqualToSize(CGSizeZero, size))
  41. {
  42. NSData* data = [NSData dataWithContentsOfURL:URL];
  43. UIImage* image = [UIImage imageWithData:data];
  44. if(image)
  45. {
  46. //如果未使用SDWebImage,则忽略;缓存该图片
  47. #ifdef dispatch_main_sync_safe
  48. [[SDImageCache sharedImageCache] storeImage:image recalculateFromImage:YES imageData:data forKey:URL.absoluteString toDisk:YES];
  49. #endif
  50. size = image.size;
  51. }
  52. }
  53. //过滤掉不符合大小的图片,大图太大浪费流量,用户体验不好
  54. if (size.height > 2048 || size.height <= 0 || size.width > 2048 || size.width <= 0 ) {
  55. return CGSizeZero;
  56. }
  57. else
  58. {
  59. return size;
  60. }
  61. }
  62. +(CGSize)downloadPNGImageSizeWithRequest:(NSMutableURLRequest*)request
  63. {
  64. [request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
  65. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  66. if(data.length == 8)
  67. {
  68. int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
  69. [data getBytes:&w1 range:NSMakeRange(0, 1)];
  70. [data getBytes:&w2 range:NSMakeRange(1, 1)];
  71. [data getBytes:&w3 range:NSMakeRange(2, 1)];
  72. [data getBytes:&w4 range:NSMakeRange(3, 1)];
  73. int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
  74. int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
  75. [data getBytes:&h1 range:NSMakeRange(4, 1)];
  76. [data getBytes:&h2 range:NSMakeRange(5, 1)];
  77. [data getBytes:&h3 range:NSMakeRange(6, 1)];
  78. [data getBytes:&h4 range:NSMakeRange(7, 1)];
  79. int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
  80. return CGSizeMake(w, h);
  81. }
  82. return CGSizeZero;
  83. }
  84. +(CGSize)downloadGIFImageSizeWithRequest:(NSMutableURLRequest*)request
  85. {
  86. [request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
  87. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  88. if(data.length == 4)
  89. {
  90. short w1 = 0, w2 = 0;
  91. [data getBytes:&w1 range:NSMakeRange(0, 1)];
  92. [data getBytes:&w2 range:NSMakeRange(1, 1)];
  93. short w = w1 + (w2 << 8);
  94. short h1 = 0, h2 = 0;
  95. [data getBytes:&h1 range:NSMakeRange(2, 1)];
  96. [data getBytes:&h2 range:NSMakeRange(3, 1)];
  97. short h = h1 + (h2 << 8);
  98. return CGSizeMake(w, h);
  99. }
  100. return CGSizeZero;
  101. }
  102. +(CGSize)downloadJPGImageSizeWithRequest:(NSMutableURLRequest*)request
  103. {
  104. [request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
  105. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  106. if ([data length] <= 0x58) {
  107. return CGSizeZero;
  108. }
  109. if ([data length] < 210) {// 肯定只有一个DQT字段
  110. short w1 = 0, w2 = 0;
  111. [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
  112. [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
  113. short w = (w1 << 8) + w2;
  114. short h1 = 0, h2 = 0;
  115. [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
  116. [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
  117. short h = (h1 << 8) + h2;
  118. return CGSizeMake(w, h);
  119. } else {
  120. short word = 0x0;
  121. [data getBytes:&word range:NSMakeRange(0x15, 0x1)];
  122. if (word == 0xdb) {
  123. [data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
  124. if (word == 0xdb) {// 两个DQT字段
  125. short w1 = 0, w2 = 0;
  126. [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
  127. [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
  128. short w = (w1 << 8) + w2;
  129. short h1 = 0, h2 = 0;
  130. [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
  131. [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
  132. short h = (h1 << 8) + h2;
  133. return CGSizeMake(w, h);
  134. } else {// 一个DQT字段
  135. short w1 = 0, w2 = 0;
  136. [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
  137. [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
  138. short w = (w1 << 8) + w2;
  139. short h1 = 0, h2 = 0;
  140. [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
  141. [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
  142. short h = (h1 << 8) + h2;
  143. return CGSizeMake(w, h);
  144. }
  145. } else {
  146. return CGSizeZero;
  147. }
  148. }
  149. }
复制代码

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



回复

使用道具 举报