2011-01-19 33 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

相關問題