0

I am trying to pass a camera overlay function as a dependency service into my shared code using the Media Plugin for Xamarin https://github.com/jamesmontemagno/MediaPlugin.

I can not figure out how to implement the dependency service correctly. The app runs but when I open the camera, it doesn't display the overlay. If someone could help me with my code, or direct me to an example of using the overlay option, I would really appreciate it.

My interface code:

public interface IPhotoOverlay 
{
   object GetImageOverlayAsync();
}

My iOS code:

public object GetImageOverlayAsync()
    {
        Func<object> func = CreateOverlay;

        return func;
    }

    public object CreateOverlay()
    {
        var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
        imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

        var screen = UIScreen.MainScreen.Bounds;
        imageView.Frame = screen;

        return imageView;
    }

My shared code:

var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions() {
            OverlayViewProvider = DependencyService.Get<IPhotoOverlay>().GetImageOverlayAsync,
            DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front});
zack
  • 85
  • 9
  • 1
    Did you register your PhotoOverlay service to your IPhotoOverlay service in the FinishedLaunching method of AppDelegeate of your iOS project? – rleffler Jan 24 '19 at 19:31
  • No I haven't, how would I go about doing that? I did not see a registration inside of the AppDelegate in the sample I found in the Xamarin documentation. – zack Jan 24 '19 at 20:16
  • I just tried adding Xamarin.Forms.DependencyService.Register(); and that did not work for me. How would I accurately register it? – zack Jan 24 '19 at 20:31
  • 1
    in your finished launching method add this line:DependencyService.Register(); and that is assuming your ios service is called PhotoOverlay. You can also register a dependency by attribute above the class – rleffler Jan 24 '19 at 21:52

1 Answers1

1

In your Xamarin.iOS Service, you need to register the Dependency, here is an example.

[assembly: Dependency (typeof (PhotoOverlayiOS))]
namespace UsingDependencyService.iOS
{
    public class PhotoOverlayiOS : IPhotoOverlay
    {

        public object GetImageOverlayAsync()
        {
            Func<object> func = CreateOverlay;

            return func;
        }

        public object CreateOverlay()
        {
            var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
            imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

            var screen = UIScreen.MainScreen.Bounds;
            imageView.Frame = screen;

            return imageView;
        }  
    }
}
Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45