UIImage+Utility.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. //
  2. // UIImage+Utility.m
  3. // XHImageViewer
  4. //
  5. // Created by 曾 宪华 on 14-2-18.
  6. // Copyright (c) 2014年 曾宪华 开发团队(http://iyilunba.com ) 本人QQ:543413507 本人QQ群(142557668). All rights reserved.
  7. //
  8. #import "UIImage+Utility.h"
  9. @implementation UIImage (Utility)
  10. + (UIImage *)decode:(UIImage *)image {
  11. if(image==nil) { return nil; }
  12. UIGraphicsBeginImageContext(image.size);
  13. {
  14. [image drawAtPoint:CGPointMake(0, 0)];
  15. image = UIGraphicsGetImageFromCurrentImageContext();
  16. }
  17. UIGraphicsEndImageContext();
  18. return image;
  19. }
  20. + (UIImage *)fastImageWithData:(NSData *)data {
  21. UIImage *image = [UIImage imageWithData:data];
  22. return [self decode:image];
  23. }
  24. + (UIImage *)fastImageWithContentsOfFile:(NSString *)path {
  25. UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];
  26. return [self decode:image];
  27. }
  28. + (UIImage* )rotateImage:(UIImage *)image {
  29. int kMaxResolution = 1080;
  30. // Or whatever
  31. CGImageRef imgRef = image.CGImage;
  32. CGFloat width = CGImageGetWidth(imgRef);
  33. CGFloat height = CGImageGetHeight(imgRef);
  34. CGAffineTransform transform = CGAffineTransformIdentity;
  35. CGRect bounds = CGRectMake(0, 0, width, height);
  36. if (width > kMaxResolution || height > kMaxResolution) {
  37. CGFloat ratio = width / height;
  38. if (ratio > 1 ) {
  39. bounds.size.width = kMaxResolution;
  40. bounds.size.height = bounds.size.width / ratio;
  41. }
  42. else {
  43. bounds.size.height = kMaxResolution;
  44. bounds.size.width = bounds.size.height * ratio;
  45. }
  46. }
  47. CGFloat scaleRatio = bounds.size.width / width;
  48. CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
  49. CGFloat boundHeight;
  50. UIImageOrientation orient = image.imageOrientation;
  51. switch (orient) {
  52. case UIImageOrientationUp:
  53. //EXIF = 1
  54. transform = CGAffineTransformIdentity;
  55. break;
  56. case UIImageOrientationUpMirrored:
  57. //EXIF = 2
  58. transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
  59. transform = CGAffineTransformScale(transform, -1.0, 1.0 );
  60. break;
  61. case UIImageOrientationDown:
  62. //EXIF = 3
  63. transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
  64. transform = CGAffineTransformRotate(transform, M_PI);
  65. break;
  66. case UIImageOrientationDownMirrored:
  67. //EXIF = 4
  68. transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
  69. transform = CGAffineTransformScale(transform, 1.0, -1.0);
  70. break;
  71. case UIImageOrientationLeftMirrored:
  72. //EXIF = 5
  73. boundHeight = bounds.size.height;
  74. bounds.size.height = bounds.size.width;
  75. bounds.size.width = boundHeight;
  76. transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width );
  77. transform = CGAffineTransformScale(transform, -1.0, 1.0);
  78. transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0 );
  79. break;
  80. case UIImageOrientationLeft:
  81. //EXIF = 6
  82. boundHeight = bounds.size.height;
  83. bounds.size.height = bounds.size.width;
  84. bounds.size.width = boundHeight;
  85. transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
  86. transform = CGAffineTransformRotate( transform, 3.0 * M_PI / 2.0 );
  87. break;
  88. case UIImageOrientationRightMirrored:
  89. //EXIF = 7
  90. boundHeight = bounds.size.height;
  91. bounds.size.height = bounds.size.width;
  92. bounds.size.width = boundHeight;
  93. transform = CGAffineTransformMakeScale(-1.0, 1.0);
  94. transform = CGAffineTransformRotate( transform, M_PI / 2.0);
  95. break;
  96. case UIImageOrientationRight:
  97. //EXIF = 8
  98. boundHeight = bounds.size.height;
  99. bounds.size.height = bounds.size.width;
  100. bounds.size.width = boundHeight;
  101. transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
  102. transform = CGAffineTransformRotate(transform, M_PI / 2.0 );
  103. break;
  104. default:
  105. [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
  106. }
  107. UIGraphicsBeginImageContext(bounds.size);
  108. CGContextRef context = UIGraphicsGetCurrentContext();
  109. if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
  110. CGContextScaleCTM(context, -scaleRatio, scaleRatio);
  111. CGContextTranslateCTM(context, -height, 0);
  112. }
  113. else {
  114. CGContextScaleCTM(context, scaleRatio, -scaleRatio);
  115. CGContextTranslateCTM(context, 0, -height);
  116. }
  117. CGContextConcatCTM(context, transform );
  118. CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
  119. UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
  120. UIGraphicsEndImageContext();
  121. return imageCopy;
  122. }
  123. @end