zoukankan      html  css  js  c++  java
  • 相册里的照片是有方向,再根据图片的方向,再反回来就好了!

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

      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. }
  • 相关阅读:
    Entity Framework 6 Recipes 2nd Edition(9-4)译->Web API 的客户端实现修改跟踪
    Entity Framework 6 Recipes 2nd Edition(9-3)译->找出Web API中发生了什么变化
    Entity Framework 6 Recipes 2nd Edition(9-2)译->用WCF更新单独分离的实体
    Entity Framework 6 Recipes 2nd Edition(9-1)译->用Web Api更新单独分离的实体
    jar包和war包的介绍和区别
    软件设计-高内聚耦合(转)
    Android中的dp,px以及wrap_content的实际展示效果
    Eclipse编辑器样式修改
    对TextView设置drawable,用setCompoundDrawables方法实现
    Android调用本机应用市场,实现应用评分功能
  • 原文地址:https://www.cnblogs.com/muyushifang07/p/4793574.html
Copyright © 2011-2022 走看看