我正在尝试为 Xamarin 中的媒体插件实现覆盖选项,可在此处找到:https://github.com/jamesmontemagno/MediaPlugin
我了解如何在iOS代码中使用该功能,但在共享代码中实现它时遇到问题。
如果有人知道我的代码出了什么问题,或者如果有在共享代码中使用覆盖功能的工作示例,将不胜感激。
我已经设置了一个接口来处理iOS代码的覆盖,并通过依赖服务传递了该函数。当我设置断点并单步执行函数时,func 内部的代码块似乎跳过了。相机工作正常,但使用相机时不显示叠加层。
IPhoto覆盖:
namespace Camera
{
public interface IPhotoOverlay
{
object GetImageOverlay();
}
}
应用代表:
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:
[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;
}
}
}
共享代码:
var photo = await Plugin.Media.CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions()
{
OverlayViewProvider = DependencyService.Get<IPhotoOverlay>().GetImageOverlay,
DefaultCamera = Plugin.Media.Abstractions.CameraDevice.Front
});
解决方案:
您可以参考以下代码。
在表单中
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,
});
}
在 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;
}