简体   繁体   中英

How to Implement Dependency Service for Camera Overlay in Xamarin.Forms Using the Media Plugin?

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});

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;
        }  
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM