Xamarin 表单:jamesmontemagno/MediaPlugin:所选图片在 IOS 应用程序中添加到 UI 时正在旋转

Xamarin forms: jamesmontemagno/MediaPlugin: Selected picture is rotating when added to UI in IOS app

我关注 this 博客是为了从画廊和相机拍照。但是当IOS中的UI时,选择的图片显示为右旋转形式。 只有在使用相机时才会出现问题,我对图库没有任何问题。此功能在 android 和 UWP 中运行良好。

截图如下:

相机代码:

async void CameraClick()
        {
            try
            {
                await CrossMedia.Current.Initialize();
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert("Camera", "No camera available.", "OK");
                    return;
                }
                _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "Sample",
                    Name = "test.jpg",
                    AllowCropping = true
                });
                if (_mediaFile == null)
                    return;
                profileImage.Source = ImageSource.FromStream(() =>
                {
                    isPicture = true;
                    return _mediaFile.GetStream();
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
            }
        }

设备:IOSSE

媒体插件版本:3.1.1

我用来显示图像的控件:Xam.plugins.Forms.Imagecircle 2.0.2(对于 android 和 UWP,我使用的是 1.8.1)

画廊图片工作正常,问题仅出现在使用相机拍照时。 android 和 UWP 部分没有问题。

原因:

即使在 Xamarin 之外,这也是一种常见的体验。这是由 iOS.

引起的

A UIImage has a property imageOrientation, which instructs the UIImageView and other UIImage consumers to rotate the raw image data. There's a good chance that this flag is being saved to the exif data in the uploaded jpeg image, but the program you use to view it is not honoring that flag.

解法:

在Github中jamesmontemagno/MediaPlugin中的Issues部分,有几个问题和你遇到的问题一样。似乎使用 GetStreamWithImageRotatedForExternalStorage 可以解决这个问题。

您可以参考: https://github.com/jamesmontemagno/MediaPlugin/issues/333

另一种方法,你可以自己旋转图像。

以下链接可能对您有所帮助:

iOS Image Orientation has Strange Behavior

iOS UIImagePickerController result image orientation after upload

iOS: Image get rotated 90 degree after saved as PNG representation data

我在过去几个月 iOS 遇到了这个问题。 解决方案是在您的代码中再添加一行:- SaveMetaData = false,

async void CameraClick()
        {
            try
            {
                await CrossMedia.Current.Initialize();
                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert("Camera", "No camera available.", "OK");
                    return;
                }
                _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                {
                    Directory = "Sample",
                    Name = "test.jpg",
                    AllowCropping = true,
                    SaveMetaData = false
                });
                if (_mediaFile == null)
                    return;
                profileImage.Source = ImageSource.FromStream(() =>
                {
                    isPicture = true;
                    return _mediaFile.GetStream();
                });
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
            }
        }