UIImage+getSize.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. //
  2. // UIImage+getSize.m
  3. // LiDa
  4. //
  5. // Created by LF on 2017/7/31.
  6. // Copyright © 2017年 Simon. All rights reserved.
  7. //
  8. #import "UIImage+getSize.h"
  9. #import <ImageIO/ImageIO.h>
  10. @implementation UIImage (getSize)
  11. // 根据图片url获取图片尺寸
  12. +(CGSize )getImageSizeWithUrl:(id)imageURL {
  13. NSURL * url = nil;
  14. if ([imageURL isKindOfClass:[NSURL class]]) {
  15. url = imageURL;
  16. }
  17. if ([imageURL isKindOfClass:[NSString class]]) {
  18. url = [NSURL URLWithString:imageURL];
  19. }
  20. if (!imageURL) {
  21. return CGSizeZero;
  22. }
  23. CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
  24. CGFloat width = 0, height = 0;
  25. if (imageSourceRef) {
  26. CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
  27. if (imageProperties != NULL) {
  28. CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
  29. if (widthNumberRef != NULL) {
  30. CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
  31. }
  32. CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
  33. if (heightNumberRef != NULL) {
  34. CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
  35. }
  36. CFRelease(imageProperties);
  37. }
  38. CFRelease(imageSourceRef);
  39. }
  40. return CGSizeMake(width, height);
  41. // NSURL* URL = nil;
  42. // if([imageURL isKindOfClass:[NSURL class]]) {
  43. // URL = imageURL;
  44. // }
  45. // if([imageURL isKindOfClass:[NSString class]]) {
  46. // URL = [NSURL URLWithString:imageURL];
  47. // }
  48. // if(URL == nil) {
  49. // // url不正确返回CGSizeZero
  50. // return CGSizeZero;
  51. // }
  52. // NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:URL];
  53. // NSString* pathExtendsion = [URL.pathExtension lowercaseString];
  54. //
  55. // CGSize size = CGSizeZero;
  56. // if([pathExtendsion isEqualToString:@"png"]) {
  57. // size = [self getPNGImageSizeWithRequest:request];
  58. // }
  59. // else if([pathExtendsion isEqual:@"gif"])
  60. // {
  61. // size = [self getGIFImageSizeWithRequest:request];
  62. // }
  63. // else{
  64. // size = [self getJPGImageSizeWithRequest:request];
  65. // }
  66. // if(CGSizeEqualToSize(CGSizeZero, size)) {
  67. //
  68. // // 如果获取文件头信息失败,发送异步请求请求原图
  69. //
  70. // NSData* data = [NSURLConnection sendSynchronousRequest:[NSURLRequest requestWithURL:URL] returningResponse:nil error:nil];
  71. // UIImage* image = [UIImage imageWithData:data];
  72. // if(image) {
  73. // size = image.size;
  74. // }
  75. // }
  76. // return size;
  77. }
  78. // 获取PNG图片的大小
  79. +(CGSize)getPNGImageSizeWithRequest:(NSMutableURLRequest*)request
  80. {
  81. [request setValue:@"bytes=16-23" forHTTPHeaderField:@"Range"];
  82. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  83. if(data.length == 8)
  84. {
  85. int w1 = 0, w2 = 0, w3 = 0, w4 = 0;
  86. [data getBytes:&w1 range:NSMakeRange(0, 1)];
  87. [data getBytes:&w2 range:NSMakeRange(1, 1)];
  88. [data getBytes:&w3 range:NSMakeRange(2, 1)];
  89. [data getBytes:&w4 range:NSMakeRange(3, 1)];
  90. int w = (w1 << 24) + (w2 << 16) + (w3 << 8) + w4;
  91. int h1 = 0, h2 = 0, h3 = 0, h4 = 0;
  92. [data getBytes:&h1 range:NSMakeRange(4, 1)];
  93. [data getBytes:&h2 range:NSMakeRange(5, 1)];
  94. [data getBytes:&h3 range:NSMakeRange(6, 1)];
  95. [data getBytes:&h4 range:NSMakeRange(7, 1)];
  96. int h = (h1 << 24) + (h2 << 16) + (h3 << 8) + h4;
  97. return CGSizeMake(w, h);
  98. }
  99. return CGSizeZero;
  100. }
  101. // 获取gif图片的大小
  102. +(CGSize)getGIFImageSizeWithRequest:(NSMutableURLRequest*)request
  103. {
  104. [request setValue:@"bytes=6-9" forHTTPHeaderField:@"Range"];
  105. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  106. if(data.length == 4)
  107. {
  108. short w1 = 0, w2 = 0;
  109. [data getBytes:&w1 range:NSMakeRange(0, 1)];
  110. [data getBytes:&w2 range:NSMakeRange(1, 1)];
  111. short w = w1 + (w2 << 8);
  112. short h1 = 0, h2 = 0;
  113. [data getBytes:&h1 range:NSMakeRange(2, 1)];
  114. [data getBytes:&h2 range:NSMakeRange(3, 1)];
  115. short h = h1 + (h2 << 8);
  116. return CGSizeMake(w, h);
  117. }
  118. return CGSizeZero;
  119. }
  120. // 获取jpg图片的大小
  121. +(CGSize)getJPGImageSizeWithRequest:(NSMutableURLRequest*)request
  122. {
  123. [request setValue:@"bytes=0-209" forHTTPHeaderField:@"Range"];
  124. NSData* data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  125. if ([data length] <= 0x58) {
  126. return CGSizeZero;
  127. }
  128. if ([data length] < 210) {// 肯定只有一个DQT字段
  129. short w1 = 0, w2 = 0;
  130. [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
  131. [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
  132. short w = (w1 << 8) + w2;
  133. short h1 = 0, h2 = 0;
  134. [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
  135. [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
  136. short h = (h1 << 8) + h2;
  137. return CGSizeMake(w, h);
  138. } else {
  139. short word = 0x0;
  140. [data getBytes:&word range:NSMakeRange(0x15, 0x1)];
  141. if (word == 0xdb) {
  142. [data getBytes:&word range:NSMakeRange(0x5a, 0x1)];
  143. if (word == 0xdb) {// 两个DQT字段
  144. short w1 = 0, w2 = 0;
  145. [data getBytes:&w1 range:NSMakeRange(0xa5, 0x1)];
  146. [data getBytes:&w2 range:NSMakeRange(0xa6, 0x1)];
  147. short w = (w1 << 8) + w2;
  148. short h1 = 0, h2 = 0;
  149. [data getBytes:&h1 range:NSMakeRange(0xa3, 0x1)];
  150. [data getBytes:&h2 range:NSMakeRange(0xa4, 0x1)];
  151. short h = (h1 << 8) + h2;
  152. return CGSizeMake(w, h);
  153. } else {// 一个DQT字段
  154. short w1 = 0, w2 = 0;
  155. [data getBytes:&w1 range:NSMakeRange(0x60, 0x1)];
  156. [data getBytes:&w2 range:NSMakeRange(0x61, 0x1)];
  157. short w = (w1 << 8) + w2;
  158. short h1 = 0, h2 = 0;
  159. [data getBytes:&h1 range:NSMakeRange(0x5e, 0x1)];
  160. [data getBytes:&h2 range:NSMakeRange(0x5f, 0x1)];
  161. short h = (h1 << 8) + h2;
  162. return CGSizeMake(w, h);
  163. }
  164. } else {
  165. return CGSizeZero;
  166. }
  167. }
  168. }
  169. @end