UIImage+RSKImageCropper.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //
  2. // UIImage+RSKImageCropper.m
  3. //
  4. // Copyright (c) 2014-present Ruslan Skorb, https://ruslanskorb.com
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. #import "UIImage+RSKImageCropper.h"
  25. @implementation UIImage (RSKImageCropper)
  26. - (UIImage *)fixOrientation
  27. {
  28. // No-op if the orientation is already correct.
  29. if (self.imageOrientation == UIImageOrientationUp) {
  30. return self;
  31. }
  32. // We need to calculate the proper transformation to make the image upright.
  33. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
  34. CGAffineTransform transform = CGAffineTransformIdentity;
  35. switch (self.imageOrientation) {
  36. case UIImageOrientationDown:
  37. case UIImageOrientationDownMirrored:
  38. transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
  39. transform = CGAffineTransformRotate(transform, M_PI);
  40. break;
  41. case UIImageOrientationLeft:
  42. case UIImageOrientationLeftMirrored:
  43. transform = CGAffineTransformTranslate(transform, self.size.width, 0);
  44. transform = CGAffineTransformRotate(transform, M_PI_2);
  45. break;
  46. case UIImageOrientationRight:
  47. case UIImageOrientationRightMirrored:
  48. transform = CGAffineTransformTranslate(transform, 0, self.size.height);
  49. transform = CGAffineTransformRotate(transform, -M_PI_2);
  50. break;
  51. case UIImageOrientationUp:
  52. case UIImageOrientationUpMirrored:
  53. break;
  54. }
  55. switch (self.imageOrientation) {
  56. case UIImageOrientationUpMirrored:
  57. case UIImageOrientationDownMirrored:
  58. transform = CGAffineTransformTranslate(transform, self.size.width, 0);
  59. transform = CGAffineTransformScale(transform, -1, 1);
  60. break;
  61. case UIImageOrientationLeftMirrored:
  62. case UIImageOrientationRightMirrored:
  63. transform = CGAffineTransformTranslate(transform, self.size.height, 0);
  64. transform = CGAffineTransformScale(transform, -1, 1);
  65. break;
  66. case UIImageOrientationUp:
  67. case UIImageOrientationDown:
  68. case UIImageOrientationLeft:
  69. case UIImageOrientationRight:
  70. break;
  71. }
  72. // Now we draw the underlying CGImage into a new context, applying the transform
  73. // calculated above.
  74. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
  75. CGImageGetBitsPerComponent(self.CGImage), 0,
  76. CGImageGetColorSpace(self.CGImage),
  77. CGImageGetBitmapInfo(self.CGImage));
  78. CGContextConcatCTM(ctx, transform);
  79. switch (self.imageOrientation) {
  80. case UIImageOrientationLeft:
  81. case UIImageOrientationLeftMirrored:
  82. case UIImageOrientationRight:
  83. case UIImageOrientationRightMirrored:
  84. CGContextDrawImage(ctx, CGRectMake(0, 0, self.size.height, self.size.width), self.CGImage);
  85. break;
  86. default:
  87. CGContextDrawImage(ctx, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);
  88. break;
  89. }
  90. // And now we just create a new UIImage from the drawing context.
  91. CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
  92. UIImage *img = [UIImage imageWithCGImage:cgimg];
  93. CGContextRelease(ctx);
  94. CGImageRelease(cgimg);
  95. return img;
  96. }
  97. - (UIImage *)rotateByAngle:(CGFloat)angleInRadians
  98. {
  99. // Calculate the size of the rotated image.
  100. CGRect rotatedImageFrame = CGRectMake(0.0, 0.0, self.size.width, self.size.height);
  101. CGSize rotatedImageSize = CGRectApplyAffineTransform(rotatedImageFrame, CGAffineTransformMakeRotation(angleInRadians)).size;
  102. // Create a bitmap-based graphics context.
  103. UIGraphicsBeginImageContextWithOptions(rotatedImageSize, NO, self.scale);
  104. CGContextRef context = UIGraphicsGetCurrentContext();
  105. // Move the origin of the user coordinate system in the context to the middle.
  106. CGContextTranslateCTM(context, rotatedImageSize.width / 2, rotatedImageSize.height / 2);
  107. // Rotates the user coordinate system in the context.
  108. CGContextRotateCTM(context, angleInRadians);
  109. // Flip the handedness of the user coordinate system in the context.
  110. CGContextScaleCTM(context, 1.0, -1.0);
  111. // Draw the image into the context.
  112. CGContextDrawImage(context, CGRectMake(-self.size.width / 2, -self.size.height / 2, self.size.width, self.size.height), self.CGImage);
  113. UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
  114. UIGraphicsEndImageContext();
  115. return rotatedImage;
  116. }
  117. @end