2011-01-19 91 views
2

好像当我在纵向模式下使用相机拍摄照片时,UIImage具有正确的大小/宽高比(1536x2048/3:4)和方向(右),导出到文件(与UIImage.AsPNG().Save()),它总是出现在横向模式(2048x1536,4:3)。UIImage数据始终处于横向模式

这是真的,还是我做错了什么?有没有解决方法,例如与ALAssetsLibrary.WriteImageToSavedPhotosAlbum

更新:原本我以为这也发生过与UIImage.SaveToPhotosAlbum(),但仔细观察后我意识到,在这种情况下,UIImage是不是相机原始的,而是一个从早期AsPNG()复原保存的数据。

再次更新:看起来这是iPhone相机的一般功能,修复它的唯一方法是手动旋转图像。我尝试将this code移植到MT,如下所示,但我似乎做了一个创造一种新方法来创建正确大小和宽高比的空白图像。任何人都可以发现错误?

public static class ImageUtils 
{ 
    static float DefaultMaxSize = 960; 

    public static UIImage ScaleAndRotateImage (UIImage image) { 
     return ScaleAndRotateImage(image, DefaultMaxSize); 
    } 

    public static UIImage ScaleAndRotateImage (UIImage image, float maxSize) 
    { 
     CGImage imgRef = image.CGImage; 

     float width = imgRef.Width; 
     float height = imgRef.Height; 

     CGAffineTransform transform = CGAffineTransform.MakeIdentity(); 

     RectangleF bounds = new RectangleF (0, 0, width, height); 
     if (width > maxSize || height > maxSize) { 
      float ratio = width/height; 
      if (ratio > 1) { 
       bounds.Width = maxSize; 
       bounds.Height = bounds.Width/ratio; 
      } else { 
       bounds.Height = maxSize; 
       bounds.Width = bounds.Height * ratio; 
      } 
     } 

     float scaleRatio = bounds.Width/width; 
     SizeF imageSize = new SizeF (imgRef.Width, imgRef.Height); 
     float boundHeight; 
     UIImageOrientation orient = image.Orientation; 
     if (orient == UIImageOrientation.Up) { 
      //EXIF = 1 
      transform = CGAffineTransform.MakeIdentity(); 
     } else if (orient == UIImageOrientation.UpMirrored) { 
      //EXIF = 2 
      transform = CGAffineTransform.MakeTranslation (imageSize.Width, 0); 
      transform.Scale (-1.0f, 1.0f); 
     } else if (orient == UIImageOrientation.Down) { 
      //EXIF = 3 
      transform = CGAffineTransform.MakeTranslation (imageSize.Width, imageSize.Height); 
      transform.Rotate ((float) Math.PI); 
     } else if (orient == UIImageOrientation.DownMirrored) { 
      //EXIF = 4 
      transform = CGAffineTransform.MakeTranslation (0, imageSize.Height); 
      transform.Scale (1.0f, -1.0f); 
     } else if (orient == UIImageOrientation.LeftMirrored) { 
      //EXIF = 5 
      boundHeight = bounds.Height; 
      bounds.Height = bounds.Width; 
      bounds.Width = boundHeight; 
      transform = CGAffineTransform.MakeTranslation (imageSize.Height, imageSize.Width); 
      transform.Scale (-1.0f, 1.0f); 
      transform.Rotate ((float)(3.0f * Math.PI/2.0)); 
     } else if (orient == UIImageOrientation.Left) { 
      //EXIF = 6 
      boundHeight = bounds.Height; 
      bounds.Height = bounds.Width; 
      bounds.Width = boundHeight; 
      transform = CGAffineTransform.MakeTranslation (0, imageSize.Width); 
      transform.Rotate ((float)(3.0f * Math.PI/2.0)); 
     } else if (orient == UIImageOrientation.RightMirrored) { 
      //EXIF = 7 
      boundHeight = bounds.Height; 
      bounds.Height = bounds.Width; 
      bounds.Width = boundHeight; 
      transform = CGAffineTransform.MakeScale (-1.0f, 1.0f); 
      transform.Rotate ((float)(Math.PI/2.0)); 
     } else if (orient == UIImageOrientation.Right) { 
      //EXIF = 8 
      boundHeight = bounds.Height; 
      bounds.Height = bounds.Width; 
      bounds.Width = boundHeight; 
      transform = CGAffineTransform.MakeTranslation (imageSize.Height, 0); 
      transform.Rotate ((float)(Math.PI/2.0)); 
     } else { 
      throw new InvalidOperationException ("Invalid image orientation"); 
     } 

     UIGraphics.BeginImageContext(bounds.Size); 

     CGContext context = UIGraphics.GetCurrentContext(); 

     if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left) { 
      context.ScaleCTM (-scaleRatio, scaleRatio); 
      context.TranslateCTM (-height, 0f); 
     } else { 
      context.ScaleCTM (scaleRatio, -scaleRatio); 
      context.TranslateCTM (0f, -height); 
     } 

     context.ConcatCTM(transform); 

     context.DrawImage (new RectangleF(0, 0, width, height), imgRef); 
     UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext(); 
     UIGraphics.EndImageContext(); 

     return imageCopy; 
    } 
} 

回答

0

尝试通过UIImageJPEGRepresentation的UIImage的NSData的要转换为PNG图像不保存图像的方向值。

+0

这个作品在相机胶卷,并在iPhoto中,但无论它使用保存图像方向必须是特定于Apple的元数据,因为当我用ImageMagick打开图像时,它仍处于横向模式。 :( – 2011-01-19 19:27:20

3

在此处找到答案:CGContextDrawImage draws image upside down when passed UIImage.CGImage:使用UIImage.Draw()而不是CGContext.DrawImage()。由此产生的代码:

public static UIImage Rotate(UIImage orig) { 
    float w0 = orig.Size.Width; 
    float h0 = orig.Size.Height; 

    UIGraphics.BeginImageContext(new SizeF(w0, h0)); 
    orig.Draw(new RectangleF(0, 0, w0, h0)); 
    UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext(); 
    UIGraphics.EndImageContext(); 

    return imageCopy; 
} 

这会产生正确大小,纵横比和方向的图像,但方向始终为“向上”。

+0

大卫,你最终得到了你的原始功能的完全工作版本吗?我不能让它按原样工作,我只是得到一个空白的图像:( – 2011-03-23 10:46:40

相关问题