3

Currently I'm developing a cross-platform app using Shared Library that able to take photos. I'm using XLabs.Forms V2.0.5782 package to do this app. I successfully developed this simple app but it only allows me to take one picture at a time.

I messed around with the codes and I managed to take multiple pictures but the problem is, the camera will be closed when I clicked 'Use Photo' and reopen again to take the next photo. What I wanted is, when I clicked 'Use Photo', the camera will reopen on the spot instead of closing and reopen.

Here's the code I did to take multiple pictures but I know it is not the right way to do it. It is in button clicked event.

IDevice device = Resolver.Resolve<IDevice>();
IMediaPicker media = device.MediaPicker;

//More codes here

async void TakePicture(object sender, System.EventArgs e)
{
    var options = new CameraMediaStorageOptions()
    {
        PercentQuality = 50,
        DefaultCamera = CameraDevice.Rear,
        MaxPixelDimension = 250
    };

    var cancel = false;

    while (!cancel)
    {
        await media.TakePhotoAsync(options).ContinueWith(t =>
        {
            if (t.IsFaulted) //If there's an error when taking photos
            {
                DisplayAlert("Error", "An error occurred when taking photo.\nPlease try again.", "OK");
            }
            else if (t.IsCanceled) //When the user click 'Cancel'
            {
                cancel = true;
            }
            else //When the user click 'Use Photo' - Here's the part where the camera will close and reopen until user click 'Cancel'
            {
                var img = ImageSource.FromStream(() => t.Result.Source);
                picList.Add(img);
            }
        });
    }

    if (picList.Count > 0)
    {
        scrollParent.IsVisible = true;
        imageScroll.Children.Clear();

        foreach (var pl in picList)
        {
            var image = new Image()
            {
                Source = pl,
                HeightRequest = 150,
                HorizontalOptions = LayoutOptions.Start,
                Aspect = Aspect.AspectFit,
                Margin = new Thickness()
                {
                    Right = 10
                }
            };

            imageScroll.Children.Add(image);
        }
    }
}

Is it possible for me to take multiple pictures with XLabs.Forms and is there a proper way? I've search everywhere but found nothing about this. Any help will be much appreciated. Thanks!

Note:

  • I'm using Visual Studio for Mac Version Preview 9 (7.0 build 2943)

  • I've only tested on iPhone running iOS 10.2. Have not tested on Android device yet

Here's a gif showing an example of my app. I think this will make you guys have a better understanding of what I want and what is happening

Please note that 3 photos are taken in this example. The camera opens up four times. After taking each photo, I clicked 'Use Photo' on the bottom right and when the camera open on the 4th time, I clicked 'Cancel' on the bottom left to stop the loop

enter image description here

Thank you!

Community
  • 1
  • 1

0 Answers0