UIImage+NTES.m 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // UIImage+NTESm
  3. // NIM
  4. //
  5. // Created by chris on 15/7/13.
  6. // Copyright (c) 2015年 Netease. All rights reserved.
  7. //
  8. #import "UIImage+NTES.h"
  9. #import "NTESDevice.h"
  10. #define ChartletBundle @"NIMDemoChartlet.bundle"
  11. #define EmojiCatalog @"default"
  12. #define ChartletChartletCatalogContentPath @"content"
  13. #define ChartletChartletCatalogIconPath @"icon"
  14. #define ChartletChartletCatalogIconsSuffixNormal @"normal"
  15. #define ChartletChartletCatalogIconsSuffixHighLight @"highlighted"
  16. @implementation UIImage (NTES)
  17. + (UIImage *)fetchImage:(NSString *)imageNameOrPath{
  18. UIImage *image = [UIImage imageNamed:imageNameOrPath];
  19. if (!image) {
  20. image = [UIImage imageWithContentsOfFile:imageNameOrPath];
  21. }
  22. return image;
  23. }
  24. + (UIImage *)fetchChartlet:(NSString *)imageName chartletId:(NSString *)chartletId{
  25. if ([chartletId isEqualToString:EmojiCatalog]) {
  26. return [UIImage imageNamed:imageName];
  27. }
  28. NSString *bundlePath = [[NSBundle mainBundle] pathForResource:ChartletBundle ofType:nil];
  29. NSString *subDirectory = [NSString stringWithFormat:@"/%@/%@",chartletId,ChartletChartletCatalogContentPath];
  30. //先拿2倍图
  31. NSString *doubleImage = [imageName stringByAppendingString:@"@2x"];
  32. NSString *tribleImage = [imageName stringByAppendingString:@"@3x"];
  33. NSString *sourcePath = [bundlePath stringByAppendingPathComponent:subDirectory];
  34. NSString *path = nil;
  35. NSArray *array = [NSBundle pathsForResourcesOfType:nil inDirectory:sourcePath];
  36. NSString *fileExt = [[array.firstObject lastPathComponent] pathExtension];
  37. if ([UIScreen mainScreen].scale == 3.0) {
  38. path = [NSBundle pathForResource:tribleImage ofType:fileExt inDirectory:sourcePath];
  39. }
  40. path = path ? path : [NSBundle pathForResource:doubleImage ofType:fileExt inDirectory:sourcePath]; //取二倍图
  41. path = path ? path : [NSBundle pathForResource:imageName ofType:fileExt inDirectory:sourcePath]; //实在没了就去取一倍图
  42. return [UIImage imageWithContentsOfFile:path];
  43. }
  44. - (UIImage *)imageForAvatarUpload
  45. {
  46. CGFloat pixels = [[NTESDevice currentDevice] suggestImagePixels];
  47. UIImage * image = [self imageForUpload:pixels];
  48. return [image fixOrientation];
  49. }
  50. #pragma mark - Private
  51. - (UIImage *)imageForUpload: (CGFloat)suggestPixels
  52. {
  53. const CGFloat kMaxPixels = 4000000;
  54. const CGFloat kMaxRatio = 3;
  55. CGFloat width = self.size.width;
  56. CGFloat height= self.size.height;
  57. //对于超过建议像素,且长宽比超过max ratio的图做特殊处理
  58. if (width * height > suggestPixels &&
  59. (width / height > kMaxRatio || height / width > kMaxRatio))
  60. {
  61. return [self scaleWithMaxPixels:kMaxPixels];
  62. }
  63. else
  64. {
  65. return [self scaleWithMaxPixels:suggestPixels];
  66. }
  67. }
  68. - (UIImage *)scaleWithMaxPixels: (CGFloat)maxPixels
  69. {
  70. CGFloat width = self.size.width;
  71. CGFloat height= self.size.height;
  72. if (width * height < maxPixels || maxPixels == 0)
  73. {
  74. return self;
  75. }
  76. CGFloat ratio = sqrt(width * height / maxPixels);
  77. if (fabs(ratio - 1) <= 0.01)
  78. {
  79. return self;
  80. }
  81. CGFloat newSizeWidth = width / ratio;
  82. CGFloat newSizeHeight= height/ ratio;
  83. return [self scaleToSize:CGSizeMake(newSizeWidth, newSizeHeight)];
  84. }
  85. //内缩放,一条变等于最长边,另外一条小于等于最长边
  86. - (UIImage *)scaleToSize:(CGSize)newSize
  87. {
  88. CGFloat width = self.size.width;
  89. CGFloat height= self.size.height;
  90. CGFloat newSizeWidth = newSize.width;
  91. CGFloat newSizeHeight= newSize.height;
  92. if (width <= newSizeWidth &&
  93. height <= newSizeHeight)
  94. {
  95. return self;
  96. }
  97. if (width == 0 || height == 0 || newSizeHeight == 0 || newSizeWidth == 0)
  98. {
  99. return nil;
  100. }
  101. CGSize size;
  102. if (width / height > newSizeWidth / newSizeHeight)
  103. {
  104. size = CGSizeMake(newSizeWidth, newSizeWidth * height / width);
  105. }
  106. else
  107. {
  108. size = CGSizeMake(newSizeHeight * width / height, newSizeHeight);
  109. }
  110. return [self drawImageWithSize:size];
  111. }
  112. - (UIImage *)drawImageWithSize: (CGSize)size
  113. {
  114. CGSize drawSize = CGSizeMake(floor(size.width), floor(size.height));
  115. // 传入的View.frame.size是0的话,直接返回nil,防止 UIGraphicsBeginImageContext() 传入0,导致崩溃
  116. if (CGSizeEqualToSize(size, CGSizeZero)) {
  117. return nil;
  118. }
  119. UIGraphicsBeginImageContext(drawSize);
  120. [self drawInRect:CGRectMake(0, 0, drawSize.width, drawSize.height)];
  121. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  122. UIGraphicsEndImageContext();
  123. return newImage;
  124. }
  125. - (UIImage *)fixOrientation
  126. {
  127. // No-op if the orientation is already correct
  128. if (self.imageOrientation == UIImageOrientationUp)
  129. return self;
  130. // We need to calculate the proper transformation to make the image upright.
  131. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
  132. CGAffineTransform transform = CGAffineTransformIdentity;
  133. switch (self.imageOrientation) {
  134. case UIImageOrientationDown:
  135. case UIImageOrientationDownMirrored:
  136. transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
  137. transform = CGAffineTransformRotate(transform, M_PI);
  138. break;
  139. case UIImageOrientationLeft:
  140. case UIImageOrientationLeftMirrored:
  141. transform = CGAffineTransformTranslate(transform, self.size.width, 0);
  142. transform = CGAffineTransformRotate(transform, M_PI_2);
  143. break;
  144. case UIImageOrientationRight:
  145. case UIImageOrientationRightMirrored:
  146. transform = CGAffineTransformTranslate(transform, 0, self.size.height);
  147. transform = CGAffineTransformRotate(transform, -M_PI_2);
  148. break;
  149. default:
  150. break;
  151. }
  152. switch (self.imageOrientation) {
  153. case UIImageOrientationUpMirrored:
  154. case UIImageOrientationDownMirrored:
  155. transform = CGAffineTransformTranslate(transform, self.size.width, 0);
  156. transform = CGAffineTransformScale(transform, -1, 1);
  157. break;
  158. case UIImageOrientationLeftMirrored:
  159. case UIImageOrientationRightMirrored:
  160. transform = CGAffineTransformTranslate(transform, self.size.height, 0);
  161. transform = CGAffineTransformScale(transform, -1, 1);
  162. break;
  163. default:
  164. break;
  165. }
  166. // Now we draw the underlying CGImage into a new context, applying the transform
  167. // calculated above.
  168. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
  169. CGImageGetBitsPerComponent(self.CGImage), 0,
  170. CGImageGetColorSpace(self.CGImage),
  171. CGImageGetBitmapInfo(self.CGImage));
  172. CGContextConcatCTM(ctx, transform);
  173. switch (self.imageOrientation) {
  174. case UIImageOrientationLeft:
  175. case UIImageOrientationLeftMirrored:
  176. case UIImageOrientationRight:
  177. case UIImageOrientationRightMirrored:
  178. // Grr...
  179. CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
  180. break;
  181. default:
  182. CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
  183. break;
  184. }
  185. // And now we just create a new UIImage from the drawing context
  186. CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
  187. UIImage *img = [UIImage imageWithCGImage:cgimg];
  188. CGContextRelease(ctx);
  189. CGImageRelease(cgimg);
  190. return img;
  191. }
  192. @end