Skip to content

Instantly share code, notes, and snippets.

@therealjohn
Created July 1, 2014 23:58
Show Gist options
  • Save therealjohn/c658d6c087847360afdc to your computer and use it in GitHub Desktop.
Save therealjohn/c658d6c087847360afdc to your computer and use it in GitHub Desktop.
UIImage FixOrientation
public static UIImage FixOrientation(this UIImage image)
{
UIImage fixedImage = image;
// No-op if the orientation is already correct
if (image.Orientation == UIImageOrientation.Up) return image;
// We need to calculate the proper transformation to make the image upright.
// We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
CGAffineTransform transform = CGAffineTransform.MakeIdentity();
switch (image.Orientation)
{
case UIImageOrientation.Down:
case UIImageOrientation.DownMirrored:
transform = CGAffineTransform.Translate(transform, image.Size.Width, image.Size.Height);
transform = CGAffineTransform.Rotate(transform, (float)Math.PI);
break;
case UIImageOrientation.Left:
case UIImageOrientation.LeftMirrored:
transform = CGAffineTransform.Translate(transform, image.Size.Width, 0);
transform = CGAffineTransform.Rotate(transform, (float)(Math.PI / 2));
break;
case UIImageOrientation.Right:
case UIImageOrientation.RightMirrored:
transform = CGAffineTransform.Translate(transform, 0, image.Size.Height);
transform = CGAffineTransform.Rotate(transform, (float)(-Math.PI / 2));
break;
case UIImageOrientation.Up:
case UIImageOrientation.UpMirrored:
break;
}
switch (image.Orientation)
{
case UIImageOrientation.UpMirrored:
case UIImageOrientation.DownMirrored:
transform = CGAffineTransform.Translate(transform, image.Size.Width, 0);
transform = CGAffineTransform.Scale(transform, -1, 1);
break;
case UIImageOrientation.LeftMirrored:
case UIImageOrientation.RightMirrored:
transform = CGAffineTransform.Translate(transform, image.Size.Height, 0);
transform = CGAffineTransform.Scale(transform, -1, 1);
break;
case UIImageOrientation.Up:
case UIImageOrientation.Down:
case UIImageOrientation.Left:
case UIImageOrientation.Right:
break;
}
// Now we draw the underlying CGImage into a new context, applying the transform
// calculated above.
using(var bitmapContext = new CGBitmapContext(IntPtr.Zero, (int)image.Size.Width, (int)image.Size.Height,
image.CGImage.BitsPerComponent, 0,
image.CGImage.ColorSpace,
image.CGImage.BitmapInfo))
{
bitmapContext.ConcatCTM(transform);
switch (image.Orientation) {
case UIImageOrientation.Left:
case UIImageOrientation.LeftMirrored:
case UIImageOrientation.Right:
case UIImageOrientation.RightMirrored:
bitmapContext.DrawImage(new RectangleF(0,0,image.Size.Height,image.Size.Width), image.CGImage);
break;
default:
bitmapContext.DrawImage(new RectangleF(0,0,image.Size.Width,image.Size.Height), image.CGImage);
break;
}
// And now we just create a new UIImage from the drawing context
using(var cgimg = bitmapContext.ToImage())
{
fixedImage = new UIImage(cgimg);
}
}
return fixedImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment