Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Open Camera app and take photo

$
0
0

Hi, I have an app for android and now I am extending it to iOS.

I need to take photos from the app. I am using native calls with dependency service (described in this thread)
From PCL I call the platform specific with this line:

var photoBytes = await DependencyService.Get<ICamera>().OpenCameraResync();

In platform specific (iOS) I have this class:

[assembly: Dependency(typeof(iOS.Controller.Camera))]
[assembly: ExportRenderer(typeof(ContentPage), typeof(iOS.Controller.Camera))]
namespace iOS.Controller
{
    public class Camera : PageRenderer, ICamera
    {
        TaskCompletionSource<byte[]> taskCompletionSource;

        public async Task<byte[]> OpenCameraResync()
        {
            var picker = new UIImagePickerController { SourceType = UIImagePickerControllerSourceType.Camera };

            picker.Canceled += (sender, e) =>
            {
                DismissViewController(true, null);
                taskCompletionSource.SetResult(null);
            };

            picker.FinishedPickingMedia += (sender, e) =>
            {
                var filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tmp.png");
                var image = (UIImage)e.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
                InvokeOnMainThread(() =>
                {
                    image.AsPNG().Save(filepath, false);
                });
                DismissViewController(true, null);
                taskCompletionSource.SetResult(null); //I will return the image as bytes here..
            };

            taskCompletionSource = new TaskCompletionSource<byte[]>();

            PresentViewController(picker, true, null);

            return await taskCompletionSource.Task;
        }
    }
}

But when I call OpenCameraResync() nothing happen, and don't see the camera UI or anything. What I am missing here?
Thanks for any help


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>