2012-09-17 85 views
4

我試圖從手機上傳圖片到我的web服務和我注意到,當我上傳圖像的圖像定位丟失。在上傳之前是否需要執行某些操作以確保圖像以正確的方向上傳?旋轉的UIImage的MonoTouch

我還研究了其他地方,發現目標-C代碼來旋轉我轉換到C#的圖像,但每一個使用旋轉方法時,圖像變黑即沒有顯示我想。

我附上我的代碼,供您參考,並會真的很感激,如果有人能告訴我什麼,我做錯了。謝謝!

public static UIImage RotateImage(this UIImage image) 
    { 
     UIImage imageToReturn = null; 
     if(image.Orientation == UIImageOrientation.Up) 
     { 
      imageToReturn = image; 
     } 
     else 
     { 
      CGAffineTransform transform = CGAffineTransform.MakeIdentity(); 

      switch (image.Orientation) { 
       case UIImageOrientation.Down: 
       case UIImageOrientation.DownMirrored: 
        transform.Translate(image.Size.Width, image.Size.Height); 
        transform.Rotate((float)Math.PI); 
        break; 

       case UIImageOrientation.Left: 
       case UIImageOrientation.LeftMirrored: 
        transform.Translate(image.Size.Width, 0); 
        transform.Rotate((float)Math.PI/2); 
        break; 

       case UIImageOrientation.Right: 
       case UIImageOrientation.RightMirrored: 
        transform.Translate(0, image.Size.Height); 
        transform.Rotate((float)-Math.PI/2); 
        break; 
       case UIImageOrientation.Up: 
       case UIImageOrientation.UpMirrored: 
        break; 
      } 

      switch (image.Orientation) { 
       case UIImageOrientation.UpMirrored: 
       case UIImageOrientation.DownMirrored: 
        transform.Translate(image.Size.Width, 0); 
        transform.Scale(-1, 1); 
        break; 

       case UIImageOrientation.LeftMirrored: 
       case UIImageOrientation.RightMirrored: 
        transform.Translate(image.Size.Height, 0); 
        transform.Scale(-1, 1); 
        break; 
       case UIImageOrientation.Up: 
       case UIImageOrientation.Down: 
       case UIImageOrientation.Left: 
       case UIImageOrientation.Right: 
        break; 
      } 

      //now draw image 
      using(var context = new CGBitmapContext(IntPtr.Zero, 
                (int)image.Size.Width, 
                (int)image.Size.Height, 
                image.CGImage.BitsPerComponent, 
                image.CGImage.BytesPerRow, 
                image.CGImage.ColorSpace, 
                image.CGImage.BitmapInfo)){ 
       context.ConcatCTM(transform); 
       switch (image.Orientation) 
       { 
        case UIImageOrientation.Left: 
        case UIImageOrientation.LeftMirrored: 
        case UIImageOrientation.Right: 
        case UIImageOrientation.RightMirrored: 
         // Grr... 
         context.DrawImage(new RectangleF(PointF.Empty,new SizeF(image.Size.Height, image.Size.Width)), image.CGImage); 
         break; 
        default: 
         context.DrawImage(new RectangleF(PointF.Empty, new SizeF(image.Size.Width, image.Size.Height)), image.CGImage); 
         break; 
       } 

       using(var imageRef = context.ToImage()) 
       { 
        imageToReturn = new UIImage(imageRef); 
       } 
      } 
     } 

     return imageToReturn; 
    } 
+1

將方向「丟失」通過上傳事實似乎表明,無論是EXIF方向標籤不再附加到圖像上,或者接收軟件不遵守它。你有沒有探索過這種可能性? –

+0

我會先探討Jacob Foshee關於EXIF方向標籤的建議。看到http://stackoverflow.com/questions/9766394/get-exif-data-from-uiimage-uiimagepickercontroller – Diego

+0

你救了我的一天。謝謝 –

回答

3

您得到黑色圖像的原因是平移和旋轉電話是倒退。 iPod/iPhone上的正常人像照片處於「正確」方向。正確的代碼將它們轉換使用:

 case UIImageOrientation.Right: 
     case UIImageOrientation.RightMirrored: 
      transform.Rotate (-(float)Math.PI/2); 
      transform.Translate (0, input.Size.Height); 
      break; 

轉換函數與順序有關。此代碼將其旋轉到正確的方向,但由於旋轉大約在左下方的0,0座標,圖像現在剛剛離開幀的底部。翻譯然後把它推到它所屬的地方。

原代碼將推動側向圖像掉幀的頂部,則該旋轉將打開的東西,以使圖像被向右轉,但方式關閉至所述框架的右側。

使用了高度較小的值,如100或200和pi/4將輕鬆展現,因爲一些原始圖像的改變這些功能的訂單時,總是會看到你的差異。

0

使用@Random提供的信息,我糾正了原來的代碼工作:

private byte[] RotateImage(UIImage image) 
    { 
     UIImage imageToReturn = null; 
     if (image.Orientation == UIImageOrientation.Up) 
     { 
      imageToReturn = image; 
     } 
     else 
     { 
      CGAffineTransform transform = CGAffineTransform.MakeIdentity(); 

      switch (image.Orientation) 
      { 
       case UIImageOrientation.Down: 
       case UIImageOrientation.DownMirrored: 
        transform.Rotate((float)Math.PI); 
        transform.Translate(image.Size.Width, image.Size.Height); 
        break; 

       case UIImageOrientation.Left: 
       case UIImageOrientation.LeftMirrored: 
        transform.Rotate((float)Math.PI/2); 
        transform.Translate(image.Size.Width, 0); 
        break; 

       case UIImageOrientation.Right: 
       case UIImageOrientation.RightMirrored: 
        transform.Rotate(-(float)Math.PI/2); 
        transform.Translate(0, image.Size.Height); 
        break; 
       case UIImageOrientation.Up: 
       case UIImageOrientation.UpMirrored: 
        break; 
      } 

      switch (image.Orientation) 
      { 
       case UIImageOrientation.UpMirrored: 
       case UIImageOrientation.DownMirrored: 
        transform.Translate(image.Size.Width, 0); 
        transform.Scale(-1, 1); 
        break; 

       case UIImageOrientation.LeftMirrored: 
       case UIImageOrientation.RightMirrored: 
        transform.Translate(image.Size.Height, 0); 
        transform.Scale(-1, 1); 
        break; 
       case UIImageOrientation.Up: 
       case UIImageOrientation.Down: 
       case UIImageOrientation.Left: 
       case UIImageOrientation.Right: 
        break; 
      } 

      //now draw image 
      using (var context = new CGBitmapContext(IntPtr.Zero, 
                (int)image.Size.Width, 
                (int)image.Size.Height, 
                image.CGImage.BitsPerComponent, 
                image.CGImage.BytesPerRow, 
                image.CGImage.ColorSpace, 
                image.CGImage.BitmapInfo)) 
      { 
       context.ConcatCTM(transform); 
       switch (image.Orientation) 
       { 
        case UIImageOrientation.Left: 
        case UIImageOrientation.LeftMirrored: 
        case UIImageOrientation.Right: 
        case UIImageOrientation.RightMirrored: 
         // Grr... 
         context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Height, (float)image.Size.Width)), image.CGImage); 
         break; 
        default: 
         context.DrawImage(new RectangleF(PointF.Empty, new SizeF((float)image.Size.Width, (float)image.Size.Height)), image.CGImage); 
         break; 
       } 

       using (var imageRef = context.ToImage()) 
       { 
        imageToReturn = new UIImage(imageRef); 
       } 
      } 
     } 

     using (NSData imageData = imageToReturn.AsJPEG()) 
     { 
      Byte[] byteArray = new Byte[imageData.Length]; 
      System.Runtime.InteropServices.Marshal.Copy(imageData.Bytes, byteArray, 0, Convert.ToInt32(imageData.Length)); 
      return byteArray; 
     } 
    } 

非常實用的一段代碼