【问题标题】:Camera image rotation issue相机图像旋转问题
【发布时间】:2013-04-06 11:10:20
【问题描述】:

我在这里遇到了一个非常奇怪的问题。当我在纵向模式下单击图像并上传它然后再次获取它时,它会逆时针旋转 90 度显示。但是当我在相机胶卷中看到它时,它会以正确的方向显示。我已经尝试了几乎所有可能的链接/代码来解决这个问题,但似乎没有任何帮助。我以 JPEG 表示形式保存图像。请帮助这个人。 提前致谢!!

【问题讨论】:

    标签: ios objective-c uiimage uiimagepickercontroller exif


    【解决方案1】:

    通过在 UIImage 上创建一个类别并根据其元数据 EXIF 缩放和旋转图像来解决。

    这是一段神奇的代码:

    - (UIImage *)scaleAndRotateImage:(UIImage *)image {
    
        int kMaxResolution = 640; // Or whatever
    
        CGImageRef imgRef = image.CGImage;
    
        CGFloat width = CGImageGetWidth(imgRef);
        CGFloat height = CGImageGetHeight(imgRef);
    
    
        CGAffineTransform transform = CGAffineTransformIdentity;
        CGRect bounds = CGRectMake(0, 0, width, height);
        if (width > kMaxResolution || height > kMaxResolution) {
            CGFloat ratio = width/height;
            if (ratio > 1) {
                bounds.size.width = kMaxResolution;
                bounds.size.height = roundf(bounds.size.width / ratio);
            }
            else {
                bounds.size.height = kMaxResolution;
                bounds.size.width = roundf(bounds.size.height * ratio);
            }
        }
    
        CGFloat scaleRatio = bounds.size.width / width;
        CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
        CGFloat boundHeight;
        UIImageOrientation orient = image.imageOrientation;
        switch(orient) {
    
            case UIImageOrientationUp: //EXIF = 1
                transform = CGAffineTransformIdentity;
                break;
    
            case UIImageOrientationUpMirrored: //EXIF = 2
                transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
                transform = CGAffineTransformScale(transform, -1.0, 1.0);
                break;
    
            case UIImageOrientationDown: //EXIF = 3
                transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
                transform = CGAffineTransformRotate(transform, M_PI);
                break;
    
            case UIImageOrientationDownMirrored: //EXIF = 4
                transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
                transform = CGAffineTransformScale(transform, 1.0, -1.0);
                break;
    
            case UIImageOrientationLeftMirrored: //EXIF = 5
                boundHeight = bounds.size.height;
                bounds.size.height = bounds.size.width;
                bounds.size.width = boundHeight;
                transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
                transform = CGAffineTransformScale(transform, -1.0, 1.0);
                transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                break;
    
            case UIImageOrientationLeft: //EXIF = 6
                boundHeight = bounds.size.height;
                bounds.size.height = bounds.size.width;
                bounds.size.width = boundHeight;
                transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
                transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
                break;
    
            case UIImageOrientationRightMirrored: //EXIF = 7
                boundHeight = bounds.size.height;
                bounds.size.height = bounds.size.width;
                bounds.size.width = boundHeight;
                transform = CGAffineTransformMakeScale(-1.0, 1.0);
                transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                break;
    
            case UIImageOrientationRight: //EXIF = 8
                boundHeight = bounds.size.height;
                bounds.size.height = bounds.size.width;
                bounds.size.width = boundHeight;
                transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
                transform = CGAffineTransformRotate(transform, M_PI / 2.0);
                break;
    
            default:
                [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
    
        }
    
        UIGraphicsBeginImageContext(bounds.size);
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
            CGContextScaleCTM(context, -scaleRatio, scaleRatio);
            CGContextTranslateCTM(context, -height, 0);
        }
        else {
            CGContextScaleCTM(context, scaleRatio, -scaleRatio);
            CGContextTranslateCTM(context, 0, -height);
        }
    
        CGContextConcatCTM(context, transform);
    
        CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
        UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return imageCopy;
    }
    
    【解决方案2】:

    试试这个:

    NSData *profileData =  UIImageJPEGRepresentation(self.profileImgView.image, 1.0); 
    

    代替:

    NSData *profileData = UIImagePNGRepresentation(profileImgView.image);
    

    希望它有效。 :)

    【讨论】:

      【解决方案3】:

      希望对您有所帮助:

      我在这里调整图像的大小以及你的问题也会解决。

      第一路:

          - (UIImage *)scaleAndRotateImage:(UIImage *)imgPic {
      
          int kMaxResolution = 650; // Or whatever    
          CGImageRef imgRef = imgPic.CGImage; 
          CGFloat width = CGImageGetWidth(imgRef);    
          CGFloat height = CGImageGetHeight(imgRef);  
          CGAffineTransform transform = CGAffineTransformIdentity;    
          CGRect bounds = CGRectMake(0, 0, width, height);
          if (width > kMaxResolution || height > kMaxResolution) {        
              CGFloat ratio = width/height;       
              if (ratio > 1) {            
                  bounds.size.width = kMaxResolution;         
                  bounds.size.height = roundf(bounds.size.width / ratio);         
              }       
              else {          
                  bounds.size.height = kMaxResolution;            
                  bounds.size.width = roundf(bounds.size.height * ratio);         
              }       
          }   
          CGFloat scaleRatio = bounds.size.width / width; 
          CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));   
          CGFloat boundHeight;    
          UIImageOrientation orient = imgPic.imageOrientation;    
          switch(orient) {            
              case UIImageOrientationUp: //EXIF = 1           
                  transform = CGAffineTransformIdentity;          
                  break;          
              case UIImageOrientationUpMirrored: //EXIF = 2           
                  transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);         
                  transform = CGAffineTransformScale(transform, -1.0, 1.0);           
                  break;          
              case UIImageOrientationDown: //EXIF = 3         
                  transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);            
                  transform = CGAffineTransformRotate(transform, M_PI);           
                  break;          
              case UIImageOrientationDownMirrored: //EXIF = 4         
                  transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
      
                  transform = CGAffineTransformScale(transform, 1.0, -1.0);
      
                  break;
      
              case UIImageOrientationLeftMirrored: //EXIF = 5
      
                  boundHeight = bounds.size.height;
      
                  bounds.size.height = bounds.size.width;
      
                  bounds.size.width = boundHeight;
      
                  transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
      
                  transform = CGAffineTransformScale(transform, -1.0, 1.0);
      
                  transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
      
                  break;
      
              case UIImageOrientationLeft: //EXIF = 6
      
                  boundHeight = bounds.size.height;
      
                  bounds.size.height = bounds.size.width;
      
                  bounds.size.width = boundHeight;
      
                  transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
      
                  transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
      
                  break;
      
              case UIImageOrientationRightMirrored: //EXIF = 7
      
                  boundHeight = bounds.size.height;
      
                  bounds.size.height = bounds.size.width;
      
                  bounds.size.width = boundHeight;
      
                  transform = CGAffineTransformMakeScale(-1.0, 1.0);
      
                  transform = CGAffineTransformRotate(transform, M_PI / 2.0);
      
                  break;
      
              case UIImageOrientationRight: //EXIF = 8
      
                  boundHeight = bounds.size.height;
      
                  bounds.size.height = bounds.size.width;
      
                  bounds.size.width = boundHeight;
      
                  transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
      
                  transform = CGAffineTransformRotate(transform, M_PI / 2.0);
      
                  break;
      
              default:
      
                  [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];
      
          }
      
          UIGraphicsBeginImageContext(bounds.size);
      
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
      
              CGContextScaleCTM(context, -scaleRatio, scaleRatio);
      
              CGContextTranslateCTM(context, -height, 0);
      
          }
      
          else {
      
              CGContextScaleCTM(context, scaleRatio, -scaleRatio);
      
              CGContextTranslateCTM(context, 0, -height);
      
          }
      
          CGContextConcatCTM(context, transform);
      
          CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
      
          UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
      
          UIGraphicsEndImageContext();
          CGContextRelease(context);
          CGImageRelease(imgRef);
          return imageCopy;
      
      }
      

      第二种方法:(如果您没有得到上述方法的答案,请使用此方法)

          +(UIImage *)resizeImage:(UIImage *)image toSize:(CGSize)destSize{
              float currentHeight = image.size.height;
              float currentWidth = image.size.width;
              float liChange ;
              CGSize newSize ;
              if(currentWidth == currentHeight) // image is square
              {
                  liChange = destSize.height / currentHeight;
                  newSize.height = currentHeight * liChange;
                  newSize.width = currentWidth * liChange;
              }
              else if(currentHeight > currentWidth) // image is landscape
                  {
                      liChange  = destSize.width / currentWidth;
                      newSize.height = currentHeight * liChange;
                      newSize.width = destSize.width;
                  }
              else                                // image is Portrait
                  {
                  liChange = destSize.height / currentHeight;
                  newSize.height= destSize.height;
                  newSize.width = currentWidth * liChange;
                  }
      
      
              UIGraphicsBeginImageContext( newSize );
              CGContextRef                context;
              UIImage                     *outputImage = nil;
      
              context = UIGraphicsGetCurrentContext();
              [image drawInRect:CGRectMake( 0, 0, newSize.width, newSize.height )];
              outputImage = UIGraphicsGetImageFromCurrentImageContext();
      
              UIGraphicsEndImageContext();
      
              CGImageRef imageRef;
              int x = (newSize.width == destSize.width) ? 0 : (newSize.width - destSize.width)/2;
              int y = (newSize.height == destSize.height) ? 0 : (newSize.height - destSize.height )/2;
              if ( ( imageRef = CGImageCreateWithImageInRect( outputImage.CGImage, CGRectMake(x, y, destSize.width, destSize.height) ) ) ) {
                  outputImage = [[UIImage alloc] initWithCGImage: imageRef] ;
              }
              CGImageRelease(imageRef);
              return  outputImage;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-07-09
        • 2013-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多