iOS UIImagePickerController result image orientation after upload

后端 未结 20 1941
长发绾君心
长发绾君心 2020-11-22 00:44

I am testing my iPhone application on an iOS 3.1.3 iPhone. I am selecting/capturing an image using a UIImagePickerController:

UIImagePickerCont         


        
20条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 01:14

    I transposed this into Xamarin:

    private static UIImage FixImageOrientation(UIImage image)
        {
            if (image.Orientation == UIImageOrientation.Up)
            {
                return image;
            }
    
            var transform = CGAffineTransform.MakeIdentity();
    
            float pi = (float)Math.PI;
    
            switch (image.Orientation)
            {
                case UIImageOrientation.Down:
                case UIImageOrientation.DownMirrored:
                    transform = CGAffineTransform.Translate(transform, image.Size.Width, image.Size.Height);
                    transform = CGAffineTransform.Rotate(transform, pi);
                    break;
    
                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                    transform = CGAffineTransform.Translate(transform, image.Size.Width, 0);
                    transform = CGAffineTransform.Rotate(transform, pi / 2);
                    break;
    
                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    transform = CGAffineTransform.Translate(transform, 0, image.Size.Height);
                    transform = CGAffineTransform.Rotate(transform, -(pi / 2));
                    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;
            }
    
            var ctx = new CGBitmapContext(null, (nint)image.Size.Width, (nint)image.Size.Height, image.CGImage.BitsPerComponent,
                image.CGImage.BytesPerRow, image.CGImage.ColorSpace, image.CGImage.BitmapInfo);
    
            ctx.ConcatCTM(transform);
    
            switch (image.Orientation)
            {
                case UIImageOrientation.Left:
                case UIImageOrientation.LeftMirrored:
                case UIImageOrientation.Right:
                case UIImageOrientation.RightMirrored:
                    ctx.DrawImage(new CGRect(0, 0, image.Size.Height, image.Size.Width), image.CGImage);
                    break;
    
                default:
                    ctx.DrawImage(new CGRect(0, 0, image.Size.Width, image.Size.Height), image.CGImage);
                    break;
            }
    
            var cgimg = ctx.ToImage();
            var img = new UIImage(cgimg);
    
            ctx.Dispose();
            ctx = null;
            cgimg.Dispose();
            cgimg = null;
    
            return img;
        }
    

提交回复
热议问题