[英]Xamarin.forms how I can add ability to take photo or select it from Gallery
I am Using Xamarin.Forms , how I can add ability to take photo or select it from Gallery.我正在使用Xamarin.Forms ,如何添加拍照功能或从图库中选择它。 I Find a lot of solutions, but no one of them is completed.
我找到了很多解决方案,但没有一个是完整的。
the Xamarin Forms Media plugin will do this, and includes sample code Xamarin Forms Media 插件将执行此操作,并包含示例代码
takePhoto.Clicked += async (sender, args) =>
{
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.PhotosSupported)
{
DisplayAlert("No Camera", ":( No camera avaialble.", "OK");
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new Media.Plugin.Abstractions.StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg"
});
if (file == null)
return;
DisplayAlert("File Location", file.Path, "OK");
image.Source = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
file.Dispose();
return stream;
});
};
This can now be easily achieved using Xamarin Essentials MediaPicker.现在可以使用 Xamarin Essentials MediaPicker 轻松实现这一点。
Here is the link to the docs and sample code for Android, iOS and UWP..这是 Android、iOS 和 UWP 的文档和示例代码的链接。
https://docs.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android https://docs.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android
var profile = new Image { };
profile.Source = "profile.png";
profile.HorizontalOptions = LayoutOptions.StartAndExpand;
profile.VerticalOptions = LayoutOptions.StartAndExpand;
var profiletap = new TapGestureRecognizer();
profiletap.Tapped += async (s, e) =>
{
var file = await CrossMedia.Current.PickPhotoAsync();
if (file == null)
return;
await DisplayAlert("File Location", file.Path, "OK");
im = ImageSource.FromStream(() =>
{
var stream = file.GetStream();
//file.Dispose();
return stream;
});
profile.Source = im;
// await Navigation.PushModalAsync(new PhotoPage(im));
};
profile.GestureRecognizers.Add(profiletap);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.