Xamarin形式:在IOS中进入UI时,相机图片仍处于旋转状态

时间:2019-02-13 14:32:27

标签: ios xamarin.forms rotation

从iOS相机拍摄的照片在用户界面中显示时向左旋转。

我已经面临此类问题,并已通过thread解决。但是在这种情况下,我仅将image path保存到列表中。

摄像机代码:

public async void OpenMyCamera()
        {
            try
            {
                List<string> images = new List<string>();
                await CrossMedia.Current.Initialize();

                if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                {
                    await DisplayAlert("Alert", "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;

                //Saving only the path to list.
                images.Add(_mediaFile.Path);

                MessagingCenter.Send<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", images);

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
            }
        }

我正在使用FlowListView使用以下代码来显示图片。

MessagingCenter.Subscribe<App, List<string>>((App)Xamarin.Forms.Application.Current, "ImagesSelected", (s, images) =>
                {
                    for (int i = 0; i < images.Count; i++)
                    {
                        _images.Add(images[i]);
                        _images = new ObservableCollection<string>(_images.Distinct());
                        listItems.FlowItemsSource = _images;
                    }
                });
            }

该问题的解决方案是什么?

1 个答案:

答案 0 :(得分:1)

解决方案:

更新

此问题已解决here

MediaPlugin的GitHub上也有discussions

当您将AllowCropping设置为true时,这似乎是一个已知问题,检查图像的exif数据,您会发现编辑的图像已经旋转了90度。如果您尚未使用图片的metadata,请尝试将其关闭以解决此问题:

var _mediaFile = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
    Directory = "Sample",
    Name = "test.jpg",
    AllowCropping = true,
    SaveMetaData = false
});

以前的答案

假设您在拍照后得到MediaFile

MediaFile file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
            {
                Directory = "Sample",
                Name = "test.jpg"
            });

然后将MediaFile转移到imageSource中:

使用GetStreamWithImageRotatedForExternalStorage将帮助您解决以下问题:  在iOS中拍照后旋转图像。

ImageSource source = ImageSource.FromStream(() =>
            {
                return file.GetStreamWithImageRotatedForExternalStorage();
            });

然后将此source添加到您的imageSource列表中:

imageSourceList.Add(source);

假设有一个带有一个属性源的模型:

public class myModel
    {
        public ImageSource source { get; set; }
    }

最后,您可以创建一个myModel的列表,将images设置为FlowItemsSource,以一个示例为例(假设有3张照片):

 var images = new List<myModel>();
 var a = new myModel {source = imageSourceList(0) };
 var b = new myModel {source = imageSourceList(1) };
 var c = new myModel {source = imageSourceList(2) };

 images.Add(a);
 images.Add(b);
 images.Add(c);

 myList.FlowItemsSource = images;

在xaml中,绑定soucre

<flv:FlowListView FlowColumnCount="3" SeparatorVisibility="None" HasUnevenRows="false" x:Name="myList">

    <flv:FlowListView.FlowColumnTemplate>
        <DataTemplate>
            <Image Source="{Binding source}" />
        </DataTemplate>
    </flv:FlowListView.FlowColumnTemplate>

</flv:FlowListView>