我有一个Xamarin forms PCL应用程序,使用的是Xam.Plugin.Media助手。我有一个页面,用户在按下按钮时调用相机助手来拍摄照片。来自相机辅助对象的字节返回到页面,并作为图像的源。页面上有一个保存按钮,我基本上是在这里调用消息传递服务并将字节保存到PCL SQlite存储中。问题是我得到了这个页面的3次成功加载,并且可以在用相机拍摄图像之后,但在它返回字节之前,使用相机助手拍摄照片。异常消息是“打开的文件太多”。这是为iOS准备的。所有相关的代码都在下面。谢谢
Camera Helper:
public class CameraHelper
{
private MediaFile file;
public async Task<byte[]> TakePicture()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
throw new Exception("No camera available");
}
using (MediaFile file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
{
Name = $"photo{DateTime.Now.ToString("yyyyMMddHHmmss")}.jpg",
PhotoSize = PhotoSize.Small,
CompressionQuality = 80,
AllowCropping = true,
}))
{
if (file == null)
{
return null;
}
using (System.IO.Stream stream = file.GetStream())
{
ImgBytes = new byte[stream.Length];
await stream.ReadAsync(ImgBytes, 0, Convert.ToInt32(stream.Length));
file.Dispose();
}
}
return ImgBytes;
}
}拍照页面:
<ContentPage.Content>
<StackLayout VerticalOptions="FillAndExpand" Padding="12,10,12,15">
<Label x:Name="photoTypeLabel" Text="Take *photo type* Photo" VerticalOptions="Start" />
<StackLayout Padding="0,30,0,70">
<ffimageloading:CachedImage x:Name="Image" Grid.Row="0" FadeAnimationEnabled="true" Aspect="AspectFill"
HeightRequest="200" WidthRequest="125" >
<ffimageloading:CachedImage.GestureRecognizers>
<TapGestureRecognizer Tapped="OnImageTapped" />
</ffimageloading:CachedImage.GestureRecognizers>
</ffimageloading:CachedImage>
</StackLayout>
<Label Grid.Row="0" Grid.Column="1"
Text="{ x:Static local:GrialShapesFont.PhotoCamera }"
Style="{StaticResource FontIcon}"
HorizontalTextAlignment="Center"
Opacity="1"
FontSize="60"
TextColor="#FF000000"
VerticalOptions="Center"
HorizontalOptions="Center">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="OnCameraTapped" />
</Label.GestureRecognizers>
</Label>
<Button Style="{StaticResource PrimaryActionButtonStyle}" VerticalOptions="End" Text="Save" WidthRequest="{ artina:OnOrientationDouble
LandscapePhone=200,
LandscapeTablet=400 }" HorizontalOptions="{ artina:OnOrientationLayoutOptions
PortraitPhone=Fill,
LandscapePhone=Center,
PortraitTablet=Fill,
LandscapeTablet=Center }" Clicked="saveButtonClicked" />
<Button Style="{StaticResource PrimaryActionButtonStyle}" VerticalOptions="End" Text="Cancel" WidthRequest="{ artina:OnOrientationDouble
LandscapePhone=200,
LandscapeTablet=400 }" HorizontalOptions="{ artina:OnOrientationLayoutOptions
PortraitPhone=Fill,
LandscapePhone=Center,
PortraitTablet=Fill,
LandscapeTablet=Center }" Clicked="cancelButtonClicked" />
</StackLayout>
</ContentPage.Content>将照片页代码放在后面:
private async void OnCameraTapped(object sender, EventArgs args)
{
CameraHelper cameraHelper = new CameraHelper();
try
{
ImgBytes = await cameraHelper.TakePicture();
Image.Source = ImageSource.FromStream(() =>
{
return new MemoryStream(ImgBytes);
});
}
catch (Exception ex)
{
if (ex.Message == "No camera available")
{
await DisplayAlert("Error", "No camera available", "Ok");
}
else
{
await DisplayAlert("Error", "Unable to take picture.", "Ok");
}
}
}https://stackoverflow.com/questions/47757015
复制相似问题