2012-09-17 90 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; 
     } 
    } 

非常实用的一段代码