Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matthiasnagel/fe7ed96dc66310c67b45fb759cf6de8c to your computer and use it in GitHub Desktop.
Save matthiasnagel/fe7ed96dc66310c67b45fb759cf6de8c to your computer and use it in GitHub Desktop.
Extension to fix orientation of an UIImage (Sets orientation to portrait)
import UIKit
extension UIImage {
public func fixedOrientation() -> UIImage {
if imageOrientation == UIImageOrientation.up {
return self
}
var transform: CGAffineTransform = CGAffineTransform.identity
switch imageOrientation {
case UIImageOrientation.down, UIImageOrientation.downMirrored:
transform = transform.translatedBy(x: size.width, y: size.height)
transform = transform.rotated(by: CGFloat(M_PI))
break
case UIImageOrientation.left, UIImageOrientation.leftMirrored:
transform = transform.translatedBy(x: size.width, y: 0)
transform = transform.rotated(by: CGFloat(M_PI_2))
break
case UIImageOrientation.right, UIImageOrientation.rightMirrored:
transform = transform.translatedBy(x: 0, y: size.height)
transform = transform.rotated(by: CGFloat(-M_PI_2))
break
case UIImageOrientation.up, UIImageOrientation.upMirrored:
break
}
switch imageOrientation {
case UIImageOrientation.upMirrored, UIImageOrientation.downMirrored:
transform.translatedBy(x: size.width, y: 0)
transform.scaledBy(x: -1, y: 1)
break
case UIImageOrientation.leftMirrored, UIImageOrientation.rightMirrored:
transform.translatedBy(x: size.height, y: 0)
transform.scaledBy(x: -1, y: 1)
case UIImageOrientation.up, UIImageOrientation.down, UIImageOrientation.left, UIImageOrientation.right:
break
}
let ctx: CGContext = CGContext(data: nil,
width: Int(size.width),
height: Int(size.height),
bitsPerComponent: self.cgImage!.bitsPerComponent,
bytesPerRow: 0,
space: self.cgImage!.colorSpace!,
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
ctx.concatenate(transform)
switch imageOrientation {
case UIImageOrientation.left, UIImageOrientation.leftMirrored, UIImageOrientation.right, UIImageOrientation.rightMirrored:
ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
default:
ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
break
}
let cgImage: CGImage = ctx.makeImage()!
return UIImage(cgImage: cgImage)
}
}
@aaronTredrea
Copy link

Better still:

func fixImageOrientation() -> UIImage {

    guard imageOrientation != .up else {
        return self
    }
    
    var transform = CGAffineTransform.identity
    
    switch imageOrientation {
        
    case .down, .downMirrored:
        
        transform = transform.translatedBy(x: size.width, y: size.height)
        transform = transform.rotated(by: CGFloat.pi)

        
    case .left, .leftMirrored:
        
        transform = transform.translatedBy(x: size.width, y: 0)
        transform = transform.rotated(by: CGFloat.pi / 2)

    case .right, .rightMirrored:
       
        transform = transform.translatedBy(x: 0, y: size.height)
        transform = transform.rotated(by: -(CGFloat.pi / 2))

    default:
        break
    }
    
    switch imageOrientation {
        
    case .upMirrored, .downMirrored:
        
        transform.translatedBy(x: size.width, y: 0)
        transform.scaledBy(x: -1, y: 1)

    case .leftMirrored, .rightMirrored:
        
        transform.translatedBy(x: size.height, y: 0)
        transform.scaledBy(x: -1, y: 1)
        
    default:
        break
    }
    
    let ctx: CGContext = CGContext(data: nil, width: Int(size.width), height: Int(size.height), bitsPerComponent: self.cgImage!.bitsPerComponent, bytesPerRow: 0, space: self.cgImage!.colorSpace!, bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)!
    
    ctx.concatenate(transform)
    
    switch imageOrientation {
    case .left, .leftMirrored, .right, .rightMirrored:
        
        ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.height, height: size.width))
        
    default:
        ctx.draw(self.cgImage!, in: CGRect(x: 0, y: 0, width: size.width, height: size.height))
        break
    }
    
    let cgImage: CGImage = ctx.makeImage()!
    
    return UIImage(cgImage: cgImage)
}

@mrjambo
Copy link

mrjambo commented Jul 29, 2018

//Thanks It is great, I have just rewrote it in C#:

UIImage CorrectOrintation(UIImage img)
{
if ((img.Orientation == UIImageOrientation.Up))
{
return img;
}

        var transform = CGAffineTransform.MakeIdentity();


        switch (img.Orientation)
        {
            case UIImageOrientation.Down:
            case UIImageOrientation.DownMirrored:
                transform = CGAffineTransform.Translate(transform, img.Size.Width, img.Size.Height);
                transform = CGAffineTransform.Rotate(transform,(nfloat)(Math.PI));
                break;
            case UIImageOrientation.Left:
            case UIImageOrientation.LeftMirrored:
                transform = CGAffineTransform.Translate(transform, img.Size.Width, 0);
                transform = CGAffineTransform.Rotate(transform, (nfloat)(Math.PI /2));
                break;
            case UIImageOrientation.Right:
            case UIImageOrientation.RightMirrored:
                transform = CGAffineTransform.Translate(transform, 0, img.Size.Height);
                transform = CGAffineTransform.Rotate(transform, (nfloat)(-Math.PI /2));
                break;
            case UIImageOrientation.UpMirrored:                   
                break;
        }

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

            case UIImageOrientation.LeftMirrored:
            case UIImageOrientation.RightMirrored:
                transform = CGAffineTransform.Translate(transform, img.Size.Height, 0);
                transform = CGAffineTransform.Scale(transform, -1, 1);
                break;
        }

        var ctx = new CGBitmapContext(null, (nint)img.Size.Width, (nint)img.Size.Height,
                                      img.CGImage.BitsPerComponent, 0, img.CGImage.ColorSpace,
                                      CGImageAlphaInfo.PremultipliedLast);
        ctx.ConcatCTM(transform);


        switch (img.Orientation)
        {
            case UIImageOrientation.Right:
            case UIImageOrientation.RightMirrored:
            case UIImageOrientation.Left:
            case UIImageOrientation.LeftMirrored:
                ctx.DrawImage(new CGRect(0, 0, img.Size.Height, img.Size.Width),img.CGImage);
                break;
            default:
                ctx.DrawImage(new CGRect(0, 0, img.Size.Width, img.Size.Height), img.CGImage);
                break;
        }
        UIImage res = new UIImage(ctx.ToImage());


        ctx = null;
        GC.Collect();
        return res;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment