ImageHandlerObject.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // ImageHandlerObject.m
  3. // MIT_Shop
  4. //
  5. // Created by 翟玉磊 on 2018/1/26.
  6. // Copyright © 2018年 翟玉磊. All rights reserved.
  7. //
  8. #import "ImageHandlerObject.h"
  9. #define KCompressibilityFactor 1280.0f
  10. @implementation ImageHandlerObject
  11. + (UIImage *)compressImage:(UIImage *)image toByte:(NSUInteger)maxLength {
  12. // Compress by quality
  13. CGFloat compression = 1;
  14. NSData *data = UIImageJPEGRepresentation(image, compression);
  15. if (data.length < maxLength) return image;
  16. CGFloat max = 1;
  17. CGFloat min = 0;
  18. for (int i = 0; i < 6; ++i) {
  19. compression = (max + min) / 2;
  20. data = UIImageJPEGRepresentation(image, compression);
  21. if (data.length < maxLength * 0.9) {
  22. min = compression;
  23. } else if (data.length > maxLength) {
  24. max = compression;
  25. } else {
  26. break;
  27. }
  28. }
  29. UIImage *resultImage = [UIImage imageWithData:data];
  30. if (data.length < maxLength) {
  31. return resultImage;
  32. }
  33. // Compress by size
  34. NSUInteger lastDataLength = 0;
  35. while (data.length > maxLength && data.length != lastDataLength) {
  36. lastDataLength = data.length;
  37. CGFloat ratio = (CGFloat)maxLength / data.length;
  38. CGSize size = CGSizeMake((NSUInteger)(resultImage.size.width * sqrtf(ratio)),
  39. (NSUInteger)(resultImage.size.height * sqrtf(ratio))); // Use NSUInteger to prevent white blank
  40. UIGraphicsBeginImageContext(size);
  41. [resultImage drawInRect:CGRectMake(0, 0, size.width, size.height)];
  42. resultImage = UIGraphicsGetImageFromCurrentImageContext();
  43. UIGraphicsEndImageContext();
  44. data = UIImageJPEGRepresentation(resultImage, compression);
  45. }
  46. return resultImage;
  47. }
  48. #pragma mark - 压缩一张图片 最大宽高1280 类似于微信算法
  49. + (UIImage *)getJPEGImagerImg:(UIImage *)image{
  50. CGFloat oldImg_WID = image.size.width;
  51. CGFloat oldImg_HEI = image.size.height;
  52. //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比
  53. if(oldImg_WID > KCompressibilityFactor || oldImg_HEI > KCompressibilityFactor){
  54. //超过设置的最大宽度 先判断那个边最长
  55. if(oldImg_WID > oldImg_HEI){
  56. //宽度大于高度
  57. oldImg_HEI = (KCompressibilityFactor * oldImg_HEI)/oldImg_WID;
  58. oldImg_WID = KCompressibilityFactor;
  59. }else{
  60. oldImg_WID = (KCompressibilityFactor * oldImg_WID)/oldImg_HEI;
  61. oldImg_HEI = KCompressibilityFactor;
  62. }
  63. }
  64. UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];
  65. NSData *dJpeg = nil;
  66. if (UIImagePNGRepresentation(newImg)==nil) {
  67. dJpeg = UIImageJPEGRepresentation(newImg, 0.5);
  68. }else{
  69. dJpeg = UIImagePNGRepresentation(newImg);
  70. }
  71. return [UIImage imageWithData:dJpeg];
  72. }
  73. #pragma mark - 压缩多张图片 最大宽高1280 类似于微信算法
  74. + (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr{
  75. NSMutableArray *newImgArr = [NSMutableArray new];
  76. for (int i = 0; i<imageArr.count; i++) {
  77. UIImage *newImg = [self getJPEGImagerImg:imageArr[i]];
  78. [newImgArr addObject:newImg];
  79. }
  80. return newImgArr;
  81. }
  82. #pragma mark - 压缩一张图片 自定义最大宽高
  83. + (UIImage *)getJPEGImagerImg:(UIImage *)image compressibilityFactor:(CGFloat)compressibilityFactor{
  84. CGFloat oldImg_WID = image.size.width;
  85. CGFloat oldImg_HEI = image.size.height;
  86. //CGFloat aspectRatio = oldImg_WID/oldImg_HEI;//宽高比
  87. if(oldImg_WID > compressibilityFactor || oldImg_HEI > compressibilityFactor){
  88. //超过设置的最大宽度 先判断那个边最长
  89. if(oldImg_WID > oldImg_HEI){
  90. //宽度大于高度
  91. oldImg_HEI = (compressibilityFactor * oldImg_HEI)/oldImg_WID;
  92. oldImg_WID = compressibilityFactor;
  93. }else{
  94. oldImg_WID = (compressibilityFactor * oldImg_WID)/oldImg_HEI;
  95. oldImg_HEI = compressibilityFactor;
  96. }
  97. }
  98. UIImage *newImg = [self imageWithImage:image scaledToSize:CGSizeMake(oldImg_WID, oldImg_HEI)];
  99. NSData *dJpeg = nil;
  100. if (UIImagePNGRepresentation(newImg)==nil) {
  101. dJpeg = UIImageJPEGRepresentation(newImg, 0.5);
  102. }else{
  103. dJpeg = UIImagePNGRepresentation(newImg);
  104. }
  105. return [UIImage imageWithData:dJpeg];
  106. }
  107. #pragma mark - 压缩多张图片 自定义最大宽高
  108. + (NSArray *)getJPEGImagerImgArr:(NSArray *)imageArr compressibilityFactor:(CGFloat)compressibilityFactor{
  109. NSMutableArray *newImgArr = [NSMutableArray new];
  110. for (int i = 0; i<imageArr.count; i++) {
  111. UIImage *newImg = [self getJPEGImagerImg:imageArr[i] compressibilityFactor:compressibilityFactor];
  112. [newImgArr addObject:newImg];
  113. }
  114. return newImgArr;
  115. }
  116. #pragma mark - 根据宽高压缩图片
  117. + (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize{
  118. UIGraphicsBeginImageContext(newSize);
  119. [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  120. UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
  121. UIGraphicsEndImageContext();
  122. return newImage;
  123. }
  124. @end