Xamarin - MediaPicker不显示视频的iOS

本文关键字:视频 iOS 显示 MediaPicker Xamarin | 更新日期: 2023-09-27 18:13:02

我试着在我写的主要应用程序中这样做,但似乎不能让它工作。所以我创建了一个单视图应用程序来尝试并弄清楚它,但我似乎仍然卡住了。他是我的一切。

public partial class HelloViewController : UIViewController
{
    partial class VideoPickerController : UIViewController
    {
        MediaPicker videoPicker;
        readonly TaskScheduler uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
        public VideoPickerController (IntPtr handle) : base (handle)
        {
        }
        public override void DidReceiveMemoryWarning ()
        {
            // Releases the view if it doesn't have a superview.
            base.DidReceiveMemoryWarning ();
            // Release any cached data, images, etc that aren't in use.
        }
        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();
            var picker = new MediaPicker ();
            PresentViewController (picker.GetPickPhotoUI (), true, null);

        }
    }
}

基本上我得到的是一个白屏幕,什么也没发生。谢谢!

Xamarin - MediaPicker不显示视频的iOS

我认为你不需要手动呈现它。这是我使用的代码,它会自动为我呈现UI。这段代码不在它自己的视图控制器中,它只是在任何我有按钮让它拍照的视图控制器中。制作相册和拍摄视频也有类似的方法。

这里是一个链接到github页面,我有这个样例应用程序托管。

var picker = new Xamarin.Media.MediaPicker();
btnCamera.Hidden = !picker.IsCameraAvailable;
btnCamera.TouchUpInside += async (sender, e) =>
{
    try
    {
        MediaFile file = 
            await picker.TakePhotoAsync(new StoreCameraMediaOptions());
        processImage(file);
    }
    catch { }
};
btnPhoto.Hidden = !picker.PhotosSupported;
btnPhoto.TouchUpInside += async (sender, e) =>
{
    try
    {
        MediaFile file = await picker.PickPhotoAsync();
        processImage(file);
    }
    catch { }
};
private void processImage(MediaFile file)
{
    if (file != null)
    {
        viewModel.Image = file.GetStream();
        viewModel.ImagePath = file.Path;
        setImage();
    }
}
private void setImage()
{
    try
    {
        System.Diagnostics.Debug.WriteLine(viewModel.ImagePath);
        if (!string.IsNullOrEmpty(viewModel.ImagePath) &&
            System.IO.File.Exists(viewModel.ImagePath))
        {
            imgImage.Image = new UIImage(NSData.FromFile(viewModel.ImagePath));
        }
        else if (viewModel.Image != null && viewModel.Image.Length != 0)
        {
            imgImage.Image = new UIImage(NSData.FromStream(viewModel.Image));
        }
    }
    //just don't load image
    catch (Exception ex)
    {
        System.Diagnostics.Debug.WriteLine(ex.Message);
    }
}