Guest User

Untitled

a guest
Jan 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.76 KB | None | 0 0
  1. +(UIImage *)scaleAndRotateImage:(UIImage *)image //for correcting the orientation of images
  2. {
  3. int kMaxResolution = 640; // Or whatever
  4. CGImageRef imgRef = image.CGImage;
  5.  
  6. CGFloat width = CGImageGetWidth(imgRef);
  7. CGFloat height = CGImageGetHeight(imgRef);
  8.  
  9. CGAffineTransform transform = CGAffineTransformIdentity;
  10. CGRect bounds = CGRectMake(0, 0, width, height);
  11. if (width > kMaxResolution || height > kMaxResolution) {
  12. CGFloat ratio = width/height;
  13. if (ratio > 1) {
  14. bounds.size.width = kMaxResolution;
  15. bounds.size.height = bounds.size.width / ratio;
  16. }
  17. else {
  18. bounds.size.height = kMaxResolution;
  19. bounds.size.width = bounds.size.height * ratio;
  20. }
  21. }
  22.  
  23. CGFloat scaleRatio = bounds.size.width / width;
  24. CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
  25. CGFloat boundHeight;
  26. UIImageOrientation orient = image.imageOrientation;
  27. switch(orient) {
  28.  
  29. case UIImageOrientationUp: //EXIF = 1
  30. transform = CGAffineTransformIdentity;
  31. break;
  32.  
  33. case UIImageOrientationUpMirrored: //EXIF = 2
  34. transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
  35. transform = CGAffineTransformScale(transform, -1.0, 1.0);
  36. break;
  37.  
  38. case UIImageOrientationDown: //EXIF = 3
  39. transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
  40. transform = CGAffineTransformRotate(transform, M_PI);
  41. break;
  42.  
  43. case UIImageOrientationDownMirrored: //EXIF = 4
  44. transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
  45. transform = CGAffineTransformScale(transform, 1.0, -1.0);
  46. break;
  47.  
  48. case UIImageOrientationLeftMirrored: //EXIF = 5
  49. boundHeight = bounds.size.height;
  50. bounds.size.height = bounds.size.width;
  51. bounds.size.width = boundHeight;
  52. transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
  53. transform = CGAffineTransformScale(transform, -1.0, 1.0);
  54. transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
  55. break;
  56.  
  57. case UIImageOrientationLeft: //EXIF = 6
  58. boundHeight = bounds.size.height;
  59. bounds.size.height = bounds.size.width;
  60. bounds.size.width = boundHeight;
  61. transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
  62. transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
  63. break;
  64.  
  65. case UIImageOrientationRightMirrored: //EXIF = 7
  66. boundHeight = bounds.size.height;
  67. bounds.size.height = bounds.size.width;
  68. bounds.size.width = boundHeight;
  69. transform = CGAffineTransformMakeScale(-1.0, 1.0);
  70. transform = CGAffineTransformRotate(transform, M_PI / 2.0);
  71. break;
  72.  
  73. case UIImageOrientationRight: //EXIF = 8
  74. boundHeight = bounds.size.height;
  75. bounds.size.height = bounds.size.width;
  76. bounds.size.width = boundHeight;
  77. transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
  78. transform = CGAffineTransformRotate(transform, M_PI / 2.0);
  79. break;
  80.  
  81. default:
  82. [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
  83.  
  84. }
  85.  
  86. UIGraphicsBeginImageContext(bounds.size);
  87.  
  88. CGContextRef context = UIGraphicsGetCurrentContext();
  89.  
  90. if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
  91. CGContextScaleCTM(context, -scaleRatio, scaleRatio);
  92. CGContextTranslateCTM(context, -height, 0);
  93. }
  94. else {
  95. CGContextScaleCTM(context, scaleRatio, -scaleRatio);
  96. CGContextTranslateCTM(context, 0, -height);
  97. }
  98.  
  99. CGContextConcatCTM(context, transform);
  100.  
  101. CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
  102. UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
  103. UIGraphicsEndImageContext();
  104.  
  105. return imageCopy;
  106. }
Add Comment
Please, Sign In to add comment