简体   繁体   English

如何使用Xamarin.Forms的媒体插件在共享代码中实现iOS覆盖?

[英]How to Implement an iOS Overlay in Shared Code Using the Media Plugin for Xamarin.Forms?

I am trying to implement the overlay option for the media plugin in Xamarin, found here: https://github.com/jamesmontemagno/MediaPlugin 我正在尝试在Xamarin中实现媒体插件的覆盖选项,可在此处找到: https : //github.com/jamesmontemagno/MediaPlugin

I understand how to use the feature in the iOS code, but am having trouble implementing it in the shared code. 我了解如何在iOS代码中使用该功能,但是在共享代码中实现该功能时遇到了麻烦。

Would really appreciate it if anyone knows what is wrong with my code, or if there is a working example of using the overlay feature in shared code. 如果有人知道我的代码出了什么问题,或者有一个在共享代码中使用覆盖功能的有效示例,将不胜感激。

I have setup an interface to handle the overlay for the iOS code, and have passed the function via a dependency service. 我已经设置了一个接口来处理iOS代码的叠加层,并已通过依赖项服务传递了该功能。 It seems that the code block inside of func is skipping over when I set breakpoints and step into the function. 当我设置断点并进入函数时,func内的代码块似乎正在跳过。 The camera works but the overlay does not appear when using the camera. 相机可以使用,但使用相机时不会出现覆盖图。

IPhotoOverlay: IPhotoOverlay:

namespace Camera
{
public interface IPhotoOverlay 
{
   object GetImageOverlay();

    }
}

AppDelegate: AppDelegate:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        LoadApplication(new App());

        Xamarin.Forms.DependencyService.Register<IPhotoOverlay, PhotoOverlay_iOS>();

        return base.FinishedLaunching(app, options);
    }

PhotoOverlay_iOS: PhotoOverlay_iOS:

[assembly: Xamarin.Forms.Dependency (typeof (PhotoOverlay_iOS))]
namespace Camera.iOS
{
public class PhotoOverlay_iOS: IPhotoOverlay
{

    public PhotoOverlay_iOS ()
    {
    }

    public object GetImageOverlay()
    {

        Func<object> func = () =>
        {
            var imageView = new UIImageView(UIImage.FromBundle("face-template.png"));
            imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

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

            return imageView;
        };

        //Func<object> func = CreateOverlay;

        return func;
    }        

}
}

Shared Code: 共享代码:

var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
            {
                OverlayViewProvider = DependencyService.Get<IPhotoOverlay>().GetImageOverlay,
                DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front
            });

Solution: 解:

You can refer the following code . 您可以参考以下代码。

in Forms 形式

async void OpenCameraAsync()
{         
  Func<object> func = () =>
  {
    var obj = DependencyService.Get<IPhotoOverlay>().GetImageOverlay();       
    return obj;
  };

  var photo = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
  {
     OverlayViewProvider =func,
     DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front,
  });
}

in iOS project 在iOS项目中

public object GetImageOverlay()
{ 
  var imageView = new UIImageView(UIImage.FromBundle("yourimagename.png"));
  imageView.ContentMode = UIViewContentMode.ScaleAspectFit;

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

  return imageView;               
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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