相册里的照片是有方向,再根据图片的方向,再反回来就好了!

相册里的照片是有方向,再根据图片的方向,再反回来就好了!

    1. + (void)fixOrientation:(UIImage *)aImage {
    2.     if (aImage==nil || !aImage) {
    3.         return;
    4.     }
    5.     // No-op if the orientation is already correct
    6.     if (aImage.imageOrientation == UIImageOrientationUp) return;
    7.    
    8.     // We need to calculate the proper transformation to make the image upright.
    9.     // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
    10.     CGAffineTransform transform = CGAffineTransformIdentity;
    11.     UIImageOrientation orientation=aImage.imageOrientation;
    12.     int orientation_=orientation;
    13.     switch (orientation_) {
    14.         case UIImageOrientationDown:
    15.         case UIImageOrientationDownMirrored:
    16.             transform = CGAffineTransformTranslate(transform, aImage.size.width, aImage.size.height);
    17.             transform = CGAffineTransformRotate(transform, M_PI);
    18.             break;
    19.            
    20.         case UIImageOrientationLeft:
    21.         case UIImageOrientationLeftMirrored:
    22.             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
    23.             transform = CGAffineTransformRotate(transform, M_PI_2);
    24.             break;
    25.            
    26.         case UIImageOrientationRight:
    27.         case UIImageOrientationRightMirrored:
    28.             transform = CGAffineTransformTranslate(transform, 0, aImage.size.height);
    29.             transform = CGAffineTransformRotate(transform, -M_PI_2);
    30.             break;
    31.     }
    32.    
    33.     switch (orientation_) {
    34.         case UIImageOrientationUpMirrored:{
    35.        
    36.         }
    37.         case UIImageOrientationDownMirrored:
    38.             transform = CGAffineTransformTranslate(transform, aImage.size.width, 0);
    39.             transform = CGAffineTransformScale(transform, -1, 1);
    40.             break;
    41.            
    42.         case UIImageOrientationLeftMirrored:
    43.         case UIImageOrientationRightMirrored:
    44.             transform = CGAffineTransformTranslate(transform, aImage.size.height, 0);
    45.             transform = CGAffineTransformScale(transform, -1, 1);
    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.     aImage=img;
    76.     img=nil;
    77. }
posted @ 2015-09-09 08:29  沐雨十方07  阅读(246)  评论(0编辑  收藏  举报