Xamarin.forms如何添加拍照或从图库中选择照片的功能

时间:2016-05-11 12:18:09

标签: c# xamarin.forms cross-platform

我正在使用 Xamarin.Forms ,我如何添加拍照或从图库中选择照片的功能。 我找到了很多解决方案,但没有一个完成。

3 个答案:

答案 0 :(得分:2)

Xamarin表单Media plugin将执行此操作,并包含示例代码

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;
          }); 
        };

答案 1 :(得分:0)

现在可以使用 Xamarin Essentials MediaPicker 轻松实现这一点。

这是适用于 Android、iOS 和 UWP 的文档和示例代码的链接..

https://docs.microsoft.com/en-us/xamarin/essentials/media-picker?tabs=android

答案 2 :(得分:-1)

        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);