Xamarin形式:进入IOS中的UI时,相机图片仍处于旋转状态 [英] Xamarin Forms: Camera picture is left rotated when comes to UI in IOS
问题描述
在UI中显示时,从IOS相机拍摄的照片将向左旋转.
The picture taken from the IOS camera is left rotated when showing it in the UI.
I already faced this type of issue and solved by this thread. But in this case, I am saving only the image path
to List.
摄像机代码:
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
通过以下代码显示图片.
I am using FlowListView
for showing the pictures using the following code.
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;
}
});
}
该问题的解决方案是什么?
What is the solution for this issue?
推荐答案
解决方案:
更新:
此处已解决.
MediaPlugin
的GitHub
将AllowCropping
设置为true时,这似乎是一个已知问题,检查图像的exif
数据,您会发现编辑的图像已旋转了90度.如果您尚未使用图片的metadata
,请尝试将其关闭以解决该问题:
It seems this is a known issue when you set AllowCropping
to true, check the image's exif
data you will find the edited image has been rotated by 90 degrees. If you haven't used the image's metadata
, try to close it to fix that:
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中拍照后旋转图像.
Using GetStreamWithImageRotatedForExternalStorage
will help you to solve the problem of
image is rotated after taken photo in iOS.
ImageSource source = ImageSource.FromStream(() =>
{
return file.GetStreamWithImageRotatedForExternalStorage();
});
然后将此source
添加到您的imageSource
列表中:
Then add this source
to your imageSource
list:
imageSourceList.Add(source);
假设有一个带有一个属性源的模型:
Assume there is a model with one property source:
public class myModel
{
public ImageSource source { get; set; }
}
最后,您可以创建一个myModel
的列表,将images
设置为FlowItemsSource
,以示例(假设有3张照片):
At last, you can create a list of myModel
, set the images
as FlowItemsSource
, take as an example(assume there are 3 photos):
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>
这篇关于Xamarin形式:进入IOS中的UI时,相机图片仍处于旋转状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!