如何在 Xamarin.UITesting 中与本机照片交互?



我一直在尝试点击 Xamarin.iOS 中的相机胶卷,当它在我的应用程序中打开时使用 Xamarin.UITesting,但由于它是操作系统的一部分,我无法使用点击功能选择相机胶卷,然后选择图像。使用该应用程序。Repl(( ,它只是如下所示:

[CalabashRootView]                                                              
[UIWindow > ... > UILayoutContainerView]
[UINavigationTransitionView > ... > _UISizeTrackingView]
[_UIRemoteView] id: "RemoteViewBridge"
[UINavigationBar] id: "Photos"
[_UIBarBackground]
[UIImageView]
[UIVisualEffectView]
[_UIVisualEffectBackdropView]
[_UIVisualEffectSubview]
[_UINavigationBarContentView]
[_UIButtonBarStackView]
[_UIButtonBarButton] label: "Cancel"
[_UIModernBarButton] label: "Cancel"
[UIButtonLabel] label: "Cancel",  text: "Cancel"
[UILabel] label: "Photos",  text: "Photos"
[UILabel] label: "Photos",  text: "Photos"
[_UIButtonBarButton] label: "Cancel"
[_UIModernBarButton] label: "Cancel"
[UIButtonLabel] label: "Cancel",  text: "Cancel"
[UIWindow > UILayoutContainerView]
[UINavigationTransitionView > ... > UITableView]
[WelcomeScreenCustomCell]
[UITableViewCellContentView]
[UIView]

我试图通过后门将图像推送到照片选择过程中,但我似乎无法让它正常工作(这是我的AppDelegate(:

[Export("selectImageFromCameraRoll:")] // notice the colon at the end of the method name
public NSString SelectImage(NSString value)
{
UIImage photo = UIImage.FromFile("testImage.jpg");
NSData imgData = photo.AsJPEG(0.6f);
UIImage img = new UIImage(imgData);
var vc = window.RootViewController;
//not working
((UIDataViewController)vc.Handle).TakenPhoto(img);
return new NSString("true");
}

有没有人对此有任何经验,有没有办法只选择图像?

您可以使用Nuget的插件MediaPlugin

不要忘记在indo.plist中添加NSCameraUsageDescriptionNSPhotoLibraryUsageDescription,以便访问设备的相机和照片/视频库。

<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to microphone.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery.</string>

你可以更好地调用MainViewController中的代码。

async void TakePhoto()
{
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
// No camera available.
return;
}
var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
Directory = "Sample",
Name = "test.jpg"
});
if (file == null)
return;
var ImagePath = file.Path;
//... do something you want
}

最新更新