【问题标题】:How to select an image from gallery when clicking a button (Xamarin iOS)单击按钮时如何从图库中选择图像(Xamarin iOS)
【发布时间】:2017-01-13 06:04:20
【问题描述】:

我查看了这段代码 sn-p: https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/ 不幸的是,这会立即加载图库,并且不会考虑让用户先选择。

我试图给用户两个选择:

1 - 拍照 2 - 从图库中选择现有照片

这两个都是您单击的按钮。有什么想法吗?

【问题讨论】:

    标签: ios xamarin uiimagepickercontroller


    【解决方案1】:

    一种方法是显示带有Prism.IPageDialogService 的ActionSheet,以便为用户提供选择。您甚至可以这样做以获得跨平台解决方案:

    private async void AddImage() // This is your method for your button
    {
        string chosen = await _dialogService.DisplayActionSheetAsync("Which source do you want to use?", "Cancel", null, "Camera", "Galery");
        if (chosen == "Camera")
        {
            TakePhoto();
        }
        else if (chosen == "Galery")
        {
            PickPhoto();
        }
    }
    

    TakePhoto()的实现:

    private async void TakePhoto()
    {
        await CrossMedia.Current.Initialize();
    
        if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
        {
            await _dialogService.DisplayAlertAsync("Warning", "Camera not available", "OK");
            return;
        }
    
        _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
        {
            SaveToAlbum = true,
            Directory = "Sample",
            Name = "sample" + DateTime.Now.ToLocalTime().ToString("yyyyMMddHHmmss") + ".jpg"
        });
    
        if (_mediaFile == null)
        {
            return;
        }
    
        ImageButtonAddGroup = _mediaFile.Path;
    }
    

    PickPhoto()的实现:

    private async void PickPhoto()
    {
        await CrossMedia.Current.Initialize();
    
        if (!CrossMedia.Current.IsPickPhotoSupported)
        {
            await _dialogService.DisplayAlertAsync("Warning", "Camera not available", "OK");
            return;
        }
    
        _mediaFile = await CrossMedia.Current.PickPhotoAsync();
        if (_mediaFile == null)
        {
            return;
        }
        ImageButtonAddGroup = _mediaFile.Path;
    }
    

    如果您想要两个按钮而不是一个,则不需要Prism.IPageDialogService,您只需将方法TakePhoto()PickPhoto() 绑定到两个不同的按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 1970-01-01
      • 1970-01-01
      • 2020-12-22
      相关资源
      最近更新 更多