Straighten and Scale UIImage


/ Published in: Objective C
Save to your folder(s)

Usage: [self straightenAndScaleImage:imageView.image andTheMaxDimension:100];


Copy this code and paste it in your HTML
  1. - (UIImage*)straightenAndScaleImage:(UIImage *)theFullImage andTheMaxDimension:(int)maxDimension
  2. {
  3.  
  4. CGImageRef img = [theFullImage CGImage];
  5. CGFloat width = CGImageGetWidth(img);
  6. CGFloat height = CGImageGetHeight(img);
  7. CGRect bounds = CGRectMake(0, 0, width, height);
  8. CGSize size = bounds.size;
  9. if (width > maxDimension || height > maxDimension) {
  10. CGFloat ratio = width/height;
  11. if (ratio > 1.0f) {
  12. size.width = maxDimension;
  13. size.height = size.width / ratio;
  14. }
  15. else {
  16. size.height = maxDimension;
  17. size.width = size.height * ratio;
  18. }
  19. }
  20.  
  21. CGFloat scale = size.width/width;
  22. CGAffineTransform transform = orientationTransformForImage(theFullImage, &size);
  23.  
  24. //CGAffineTransform transform = orientationTransformForImage(theFullImage, &size);
  25. UIGraphicsBeginImageContext(size);
  26. CGContextRef context = UIGraphicsGetCurrentContext();
  27. // Flip
  28. UIImageOrientation orientation = [theFullImage imageOrientation];
  29. if (orientation == UIImageOrientationRight || orientation == UIImageOrientationLeft) {
  30.  
  31. CGContextScaleCTM(context, -scale, scale);
  32. CGContextTranslateCTM(context, -height, 0);
  33. }else {
  34. CGContextScaleCTM(context, scale, -scale);
  35. CGContextTranslateCTM(context, 0, -height);
  36. }
  37. CGContextConcatCTM(context, transform);
  38. CGContextDrawImage(context, bounds, img);
  39. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  40. UIGraphicsEndImageContext();
  41. return newImage;
  42. }
  43.  
  44. CGAffineTransform orientationTransformForImage(UIImage *image, CGSize *newSize) {
  45. CGImageRef img = [image CGImage];
  46. CGFloat width = CGImageGetWidth(img);
  47. CGFloat height = CGImageGetHeight(img);
  48. CGSize size = CGSizeMake(width, height);
  49. CGAffineTransform transform = CGAffineTransformIdentity;
  50. CGFloat origHeight = size.height;
  51. UIImageOrientation orient = image.imageOrientation;
  52. switch(orient) { /* EXIF 1 to 8 */
  53. case UIImageOrientationUp:
  54. break;
  55. case UIImageOrientationUpMirrored:
  56. transform = CGAffineTransformMakeTranslation(width, 0.0f);
  57. transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
  58. break;
  59. case UIImageOrientationDown:
  60. transform = CGAffineTransformMakeTranslation(width, height);
  61. transform = CGAffineTransformRotate(transform, M_PI);
  62. break;
  63. case UIImageOrientationDownMirrored:
  64. transform = CGAffineTransformMakeTranslation(0.0f, height);
  65. transform = CGAffineTransformScale(transform, 1.0f, -1.0f);
  66. break;
  67. case UIImageOrientationLeftMirrored:
  68. size.height = size.width;
  69. size.width = origHeight;
  70. transform = CGAffineTransformMakeTranslation(height, width);
  71. transform = CGAffineTransformScale(transform, -1.0f, 1.0f);
  72. transform = CGAffineTransformRotate(transform, 3.0f * M_PI / 2.0f);
  73. break;
  74. case UIImageOrientationLeft:
  75. size.height = size.width;
  76. size.width = origHeight;
  77. transform = CGAffineTransformMakeTranslation(0.0f, width);
  78. transform = CGAffineTransformRotate(transform, 3.0f * M_PI / 2.0f);
  79. break;
  80. case UIImageOrientationRightMirrored:
  81. size.height = size.width;
  82. size.width = origHeight;
  83. transform = CGAffineTransformMakeScale(-1.0f, 1.0f);
  84. transform = CGAffineTransformRotate(transform, M_PI / 2.0f);
  85. break;
  86. case UIImageOrientationRight:
  87. size.height = size.width;
  88. size.width = origHeight;
  89. transform = CGAffineTransformMakeTranslation(height, 0.0f);
  90. transform = CGAffineTransformRotate(transform, M_PI / 2.0f);
  91. break;
  92. default:
  93. ;
  94. }
  95. *newSize = size;
  96. return transform;
  97. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.