Advertisement
Guest User

Untitled

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