别忽视UIImage中的方向属性, imageOrientation

时间:2022-01-12 07:21:36
这几天写个拍照,或者从相册中选择照片,进行剪切,然后分享.结果出现了,剪切后图片颠倒或者旋转90度的问题.

找了很久才发现是忽略imageOrientation这个属性.

以下为解决方法:

[cpp]  view plain copy
  1. + (UIImage *)fixOrientation:(UIImage *)aImage {  
  2.       
  3.     // No-op if the orientation is already correct  
  4.     if (aImage.imageOrientation == UIImageOrientationUp)   
  5.         return aImage;  
  6.       
  7.     // We need to calculate the proper transformation to make the image upright.  
  8.     // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.  
  9.     CGAffineTransform transform = CGAffineTransformIdentity;  
  10.       
  11.     switch (aImage.imageOrientation) {  
  12.         case UIImageOrientationDown:  
  13.         case UIImageOrientationDownMirrored:  
  14.             transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);  
  15.             transform = CGAffineTransformRotate(transform, M_PI);  
  16.             break;  
  17.               
  18.         case UIImageOrientationLeft:  
  19.         case UIImageOrientationLeftMirrored:  
  20.             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
  21.             transform = CGAffineTransformRotate(transform, M_PI_2);  
  22.             break;  
  23.               
  24.         case UIImageOrientationRight:  
  25.         case UIImageOrientationRightMirrored:  
  26.             transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);  
  27.             transform = CGAffineTransformRotate(transform, -M_PI_2);  
  28.             break;  
  29.         default:  
  30.             break;  
  31.     }  
  32.       
  33.     switch (aImage.imageOrientation) {  
  34.         case UIImageOrientationUpMirrored:  
  35.         case UIImageOrientationDownMirrored:  
  36.             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);  
  37.             transform = CGAffineTransformScale(transform, -1, 1);  
  38.             break;  
  39.               
  40.         case UIImageOrientationLeftMirrored:  
  41.         case UIImageOrientationRightMirrored:  
  42.             transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);  
  43.             transform = CGAffineTransformScale(transform, -1, 1);  
  44.             break;  
  45.         default:  
  46.             break;  
  47.     }  
  48.       
  49.     // Now we draw the underlying CGImage into a new context, applying the transform  
  50.     // calculated above.  
  51.     CGContextRef ctx = CGBitmapContextCreate(NULL, aImage.size.width, aImage.size.height,  
  52.                                              CGImageGetBitsPerComponent(aImage.CGImage), 0,  
  53.                                              CGImageGetColorSpace(aImage.CGImage),  
  54.                                              CGImageGetBitmapInfo(aImage.CGImage));  
  55.     CGContextConcatCTM(ctx, transform);  
  56.     switch (aImage.imageOrientation) {  
  57.         case UIImageOrientationLeft:  
  58.         case UIImageOrientationLeftMirrored:  
  59.         case UIImageOrientationRight:  
  60.         case UIImageOrientationRightMirrored:  
  61.             // Grr...  
  62.             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.height,aImage.size.width), aImage.CGImage);  
  63.             break;  
  64.               
  65.         default:  
  66.             CGContextDrawImage(ctx, CGRectMake(0,0,aImage.size.width,aImage.size.height), aImage.CGImage);  
  67.             break;  
  68.     }  
  69.       
  70.     // And now we just create a new UIImage from the drawing context  
  71.     CGImageRef cgimg = CGBitmapContextCreateImage(ctx);  
  72.     UIImage *img = [UIImage imageWithCGImage:cgimg];  
  73.     CGContextRelease(ctx);  
  74.     CGImageRelease(cgimg);  
  75.     return img;  
  76. }  

参考:http://www.cnblogs.com/jiangyazhou/archive/2012/03/22/2412343.html


如果用用UIGraphicsBeginImageContext 去重新绘制也可以解决,但是处理速度实在太慢.