We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: June 24, 2019).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to resolve image rotated when loading image / after saving image in iOS device?

Platform: Xamarin.Forms |
Control: SfImageEditor |
Published Date: October 30, 2018 |
Last Revised Date: June 25, 2019

In iOS, Image is always saving in landscape (90 degree rotation) after edit the image in image editor even if image loads in portrait. The reason for this  issue was portrait images which always stored with a metadata of angle 90 irrespective of the orientation as like below screenshot.

 Portrait image stored with a angle 90

 

To avoid this issue, you have to change the orientation of the image before loading into image editor. Please refer the below steps for better understanding.

 

Step 1: Create SfImageEditor sample with all necessary assemblies.

 

Please refer the below link to create a simple SfImageEditor sample along with the ways to configure it.

 

https://help.syncfusion.com/xamarin/sfimageeditor/getting-started

 

Step 2: To change the orientation of image, you have to implement change orientation method in app delegate class in iOS Platform.

 

 

Step 3: In AppDelegate ,  you have to implement change orientation method as like below code snippet.

 

 

  
       Stream ChangeOrientation(UIImage rotatedImage)
        {
            float width = rotatedImage.CGImage.Width;
            float height = rotatedImage.CGImage.Height;
            CGImage imgRef = rotatedImage.CGImage;
            CGAffineTransform transform = CGAffineTransform.MakeIdentity();
            CGRect bounds = new CGRect(0, 0, width, height);
            float scaleRatio = (float)(bounds.Size.Width / width);
            CGSize imageSize = new CGSize(imgRef.Width, imgRef.Height);
 
            UIImageOrientation orient = rotatedImage.Orientation;
            switch (orient)
            {
 
                case UIImageOrientation.Right:
                    bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                    transform = CGAffineTransform.MakeTranslation(imageSize.Height, 0.0f);
                    transform = CGAffineTransform.Rotate(transform, (System.nfloat)(Math.PI / 2.0));
                    break;
 
                case UIImageOrientation.Up: //EXIF = 1
                    transform = CGAffineTransform.MakeIdentity();
                    break;
 
                case UIImageOrientation.UpMirrored: //EXIF = 2
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, 0.0f);
                    transform = CGAffineTransform.Scale(transform, -1.0f, 1.0f);
                    break;
 
                case UIImageOrientation.Down: //EXIF = 3
                    transform = CGAffineTransform.MakeTranslation(imageSize.Width, imageSize.Height);
                    transform = CGAffineTransform.Rotate(transform, (System.nfloat)Math.PI);
                    break;
 
                case UIImageOrientation.DownMirrored: //EXIF = 4
                    transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Height);
                    transform = CGAffineTransform.Scale(transform, 1.0f, -1.0f);
                    break;
 
                case UIImageOrientation.LeftMirrored: //EXIF = 5
                    bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                    transform = CGAffineTransform.MakeTranslation(imageSize.Height, imageSize.Width);
                    transform = CGAffineTransform.Scale(transform, -1.0f, 1.0f);
                    transform = CGAffineTransform.Rotate(transform, (System.nfloat)(3.0 * Math.PI / 2.0));
                    break;
 
                case UIImageOrientation.Left: //EXIF = 6
                    bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                    transform = CGAffineTransform.MakeTranslation(0.0f, imageSize.Width);
                    transform = CGAffineTransform.Rotate(transform, (System.nfloat)(3.0 * Math.PI / 2.0));
                    break;
 
                case UIImageOrientation.RightMirrored: //EXIF = 7
                    bounds.Size = new CGSize(bounds.Size.Height, bounds.Size.Width);
                    transform = CGAffineTransform.MakeScale(-1.0f, 1.0f);
                    transform = CGAffineTransform.Rotate(transform, (System.nfloat)(Math.PI / 2.0));
                    break;
 
            }
 
            UIGraphics.BeginImageContext(bounds.Size);
            CGContext context = UIGraphics.GetCurrentContext();
            if (orient == UIImageOrientation.Right || orient == UIImageOrientation.Left)
            {
                context.ScaleCTM(-scaleRatio, scaleRatio);
                context.TranslateCTM(-height, 0);
            }
            else
            {
                context.ScaleCTM(scaleRatio, -scaleRatio);
                context.TranslateCTM(0, -height);
            }
 
            context.ConcatCTM(transform);
            context.DrawImage(new CGRect(0, 0, width, height), imgRef);
            UIImage imageCopy = UIGraphics.GetImageFromCurrentImageContext();
            UIGraphics.EndImageContext();
            var str = imageCopy.AsPNG().AsStream();
            return str;
 
        }
    

 

Step 4: You must call the change orientation method before image loads into image editor. In change orientation method, you have to pass UIImage as argument which is get  from camera or gallery.

 

To call the change orientation method as like below code snippet.

 

 

  
var stream = ChangeOrientation(e.OriginalImage);
 

 

 

Sample link: http://www.syncfusion.com/downloads/support/directtrac/general/ze/IOSORI~1-1634555063.zip

 

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile
Live Chat Icon