Turn UIImage Monotouch

I try to upload an image from my phone to my web service and notice that the orientation of the image is lost when loading the image. Is there something I need to do before uploading so that the image is loaded in the correct orientation?

I also looked elsewhere and found objective-C code to rotate the images I converted to C #, but every time the rotate method is used, the image turns black, i.e. nothing is displayed, I think.

I am attaching my code for your reference and will really be very grateful if someone tells me what I'm doing wrong. Thanks!

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; } 
+4
source share
3 answers

The reason you get black images is to transfer and rotate calls back. Regular portrait images on the iPod / iphone are in the “correct” orientation. The correct code to convert them uses:

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

Transformation functions are order dependent. This code rotates it in the correct orientation, but since the rotation around the 0,0 coordinate is in the lower left corner, the image is now very close to the bottom of the frame. the translation then pushes it to where it belongs.

The source code will push the side image from the top of the frame, then rotation will rotate everything so that the image will be rotated to the right, but to the right of the frame.

Using a small height value, such as 100 or 200 and pi / 4, you can easily see the differences that you get when you change the order of these functions, since part of the original image will always be visible.

+3
source

I found this meaning through this question . Hope this code helps you!

0
source

Using the information provided by @Random, I fixed the source code to work:

  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; } } 

Very useful piece of code

0
source

Source: https://habr.com/ru/post/1434517/


All Articles