locked
Selected picture is not showing in UI from gallery and camera for IOS RRS feed

  • Question

  • User351573 posted

    Complete Scenario

    I have an add icon on one page, it will show camera and gallery options when tap. If choose the camera, I will open another content page and open camera there. But the captured picture is not showing in the UI. Same for the gallery, selected image from the gallery is not showing in UI. This feature is working fine in android and not working in IOS.

    Codes

    When click add icon

    string action = await DisplayActionSheet(null, "Cancel", null, "Camera", "Gallery");
                if (action == "Camera")
                {
                    await Navigation.PushModalAsync(new NewTweetPage("Camera"));
                }
                else if (action == "Gallery")
                {
                    await Navigation.PushModalAsync(new NewTweetPage("Gallery"));
                }
    

    When entering next page

    public NewTweetPage(String medium)
            {
                InitializeComponent();
                if (medium == "Camera" )
                {
                    OpenMyCamera(); 
                }
                else if(medium == "Gallery")
                {
                    OpenMygallery();
                }
            }
    
    public async void OpenMyCamera()
            {
                try
                {
                    await CrossMedia.Current.Initialize();
    
                    if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
                    {
                        await DisplayAlert("Camera", "No camera available.", "OK");
                        return;
                    }
    
                    _mediaFile = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions
                    {
                        Directory = "Sample",
                        Name = "test.jpg",
                        AllowCropping = true
                    });
    
                    if (_mediaFile == null)
                        return;
                    tweetPicture.Source = ImageSource.FromStream(() =>
                    {
                        isPicture = true;
                        return _mediaFile.GetStream();
                    });
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
                }
            }
    
    public async void OpenMygallery()
            {
                try
                {
                    await CrossMedia.Current.Initialize();
    
                    if (!CrossMedia.Current.IsPickPhotoSupported)
                    {
                        await DisplayAlert("Gallery", ":( No photos available.", "OK");
                        return;
                    }
    
                    _mediaFile = await CrossMedia.Current.PickPhotoAsync();
    
                    if (_mediaFile == null)
                        return;
                    tweetPicture.Source = ImageSource.FromStream(() =>
                    {
                        isPicture = true;
                        return _mediaFile.GetStream();
                    });
                }
                catch (Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine("Exception:>" + ex);
                }
            }
    

    The same code is working fine in profile page part, but in that case, there is no page navigation, everything is happening on the same page.

    Don't know what is the problem with the current code, please help me to solve this issue.

    Thursday, November 22, 2018 12:57 PM

Answers

  • User351573 posted

    Got Solution :) :) :)

    Putting navigation commands in the constructor cause issue. When putting them in the OnAppearing override the issue got resolved and pictures are showing in UI. Removed the if else code from the constructor and added it on the onappearing function.

    OnAppearing code:

    protected override void OnAppearing()
            {
                if (pictureMedium == "Camera")
                {
                    OpenMyCamera();
                }
                else if (pictureMedium == "Gallery")
                {
                    OpenMygallery();
                }
            }
    
    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, November 23, 2018 5:01 AM