获取网络图片的大小 改变 图片色值 灰度什么的方法集合-b

时间:2021-03-12 01:49:38

直接上代码了 头文件

  1. // 图片处理 0 半灰色  1 灰度   2 深棕色    3 反色
  2. +(UIImage*)imageWithImage:(UIImage*)image grayLevelType:(UIImageGrayLevelType)type;
  3. //色值 变暗多少 0.0 - 1.0
  4. +(UIImage*)imageWithImage:(UIImage*)image darkValue:(float)darkValue;
  5. /**
  6. 获取网络图片的Size, 先通过文件头来获取图片大小
  7. 如果失败 会下载完整的图片Data 来计算大小 所以最好别放在主线程
  8. 如果你有使用SDWebImage就会先看下 SDWebImage有缓存过改图片没有
  9. 支持文件头大小的格式 png、gif、jpg   http://www.cocoachina.com/bbs/read.php?tid=165823
  10. */
  11. +(CGSize)downloadImageSizeWithURL:(id)imageURL;

代码实现:

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

download url:// http://download.csdn.net/detail/li6185377/7368213

感谢大神分享