dimonovdd / Xamarin.MediaGallery

This plugin is designed to picking and save images and video files from native gallery of Android and iOS devices and capture photos
MIT License
148 stars 18 forks source link

CapturePhotoAsync not doing anything on iOS #119

Closed williambuchanan2 closed 1 year ago

williambuchanan2 commented 1 year ago

Description

When I call await MediaGallery.CapturePhotoAsync() nothing happens.

A bit more strange than that though - I have a common class which calls MediaGallery. Calling it from one page works fine (so I know the code works). But calling it from a different page doesn't.

The method I call is below. It gets as far as await MediaGallery.CapturePhotoAsync(). I see nothing happening when I execute this line. I can't see any exceptions or anything - the call just never returns.

        public async Task<bool> TakePhoto(string fileName, int? downsize = null)
        {
            bool result = false;
            CameraResults = new List<CameraResult>();

            try
            {
                if (MediaGallery.CheckCapturePhotoSupport())
                {
                    var status = await Permissions.RequestAsync<Permissions.Camera>();

                    if (status != PermissionStatus.Granted)
                        return false;

                    using (IMediaFile file = await MediaGallery.CapturePhotoAsync()) // Doesn't get past here
                    {
                        if (file != null)
                        {

Actual Behavior

Nothing happens

Expected behavior

Camera should open

Steps to reproduce the behavior

  1. Implement the above code
  2. Not sure why it works on one page but not the other so hard to advise what other steps to take.

Screenshots or Videos

Reproduction Link

Configuration

dimonovdd commented 1 year ago

Hi,

It looks like a problem with Rg.Plugins.Popup. Please see FAQ

williambuchanan2 commented 1 year ago

Hi

I'm not using Rg.Plugins.Popup, but the problematic call is happening from a view that I am displaying as a bottom sheet. I had already tried following the code from the FAQ, but it doesn't compile. I removed the lines which were causing an issue.

image

Any suggestions?

dimonovdd commented 1 year ago

but the problematic call is happening from a view that I am displaying as a bottom sheet

What is a bottom sheet?

global::Xamarin.Forms.Forms.Init();
LoadApplication(new App());

Are you using MAUI or Xamarin.Forms?

williambuchanan2 commented 1 year ago

Here is the link to BottomSheet:

https://github.com/VladislavAntonyuk/MauiSamples/tree/main/BottomSheet

I am using Maui.

dimonovdd commented 1 year ago

I haven't developed MAUI apps with such perversions yet. But the logic is similar to the one used in Xamarin.Forms.

You need to create your own function that returns TopViewController. Microsoft.Maui.ApplicationModel.Platform.GetCurrentUIViewController() is default for MAUI.

Sample implementation for Xamarin.Forms with Rg.Plugins.Popup:

static UIViewController? GetTopViewController()
{
    if (!(PopupNavigation.Instance?.PopupStack?.Any() ?? false))
        return Xamarin.Essentials.Platform.GetCurrentUIViewController();

        return CustomNavigationRenderer.LastController?.ViewControllers?.LastOrDefault();
 }

In Xamarin.Forms, we replaced TopViewController with the last open page:

public class RootNavigationPageRenderer : NavigationRenderer
{
    public static RootNavigationPageRenderer LastNavigationController { get; private set; }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        LastNavigationController = this;
    }
}

This is a sore topic for MAUI and Xamarin.Forms: all native windows and alerts depend on parent from which they were launched, but unfortunately access to controllers is very confusing. Microsoft.Maui.ApplicationModel and Xamarin.Essentials partially solve this problem. But they will never work fine with third-party libraries (BottomSheet or Rg.Plugins.Popup). Maybe there are workaround for MAUI somewhere, but you can definitely write it yourself.

williambuchanan2 commented 1 year ago

Ok, thanks. I went with something like the second option just for now to get me going and it seems to work now.

So, a few days in the hole, and i'm now back to the original problem of photos coming out sideways and upside down...

dimonovdd commented 1 year ago

of photos coming out sideways and upside down...

If the plugin rotates the image, it should change its metadata. After that, the image will no longer be original.

Several people promised to make PR with such functionality. They were all lost, I think they realized that the task is very specific to each application

dimonovdd commented 1 year ago

it looks like I need to make a gist with a very simple code on how to rotate a image. And add a link to it in README

williambuchanan2 commented 1 year ago

That would be great. I’ve tried to read the various comments about it but couldn’t find an example that concisely dealt with the problem for all platforms.