我有一个xamarin表单跨平台应用程序。在该应用程序中,我使用xamarin表单依赖服务从本机平台上的iOS和Android设备拍照。 Android相机工作正常但iOS相机突然出现错误和应用程序崩溃。我记录并跟踪iOS相机的问题如下。
**由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'应用程序尝试以模态方式呈现活动控制器。'
以下是用于拍照的我的iOS原生代码,它返回照片流作为结果。我使用依赖服务从我的共享xamarin表单项目中调用此方法或服务。
public Task<CameraResult> TakePictureAsync(bool cameraFlag) { var tcs = new TaskCompletionSource<CameraResult>();
Camera.TakePicture(UIApplication.SharedApplication.KeyWindow.RootViewController, (imagePickerResult) =>
{
if (imagePickerResult == null)
{
tcs.TrySetResult(null);
return;
}
var photo = imagePickerResult.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;
// You can get photo meta data with using the following
// var meta = obj.ValueForKey(new NSString("UIImagePickerControllerMediaMetadata")) as NSDictionary;
var documentsDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string imageName = Settings.AppuserUserId + "_" + Guid.NewGuid();
string jpgFilename = Path.Combine(documentsDirectory, imageName + ".jpg");
NSData imgData = photo.AsJPEG();
NSError err = null;
if (imgData.Save(jpgFilename, false, out err))
{
CameraRes
ult result = new CameraResult();
result.Picture = ImageSource.FromStream(imgData.AsStream);
result.FilePath = jpgFilename;
tcs.TrySetResult(result);
}
else
{
tcs.TrySetException(new Exception(err.LocalizedDescription));
} });
return tcs.Task; }
void OnImagePickerFinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs args)
{
UIImage image = args.EditedImage ?? args.OriginalImage;
if (image != null)
{
// Convert UIImage to .NET Stream object
CameraResult result = new CameraResult();
NSData data = image.AsJPEG();
result.Picture = ImageSource.FromStream(data.AsStream);
taskCompletionSource.SetResult(result);
}
else
{
taskCompletionSource.SetResult(null);
}
imagePicker.DismissModalViewController(true);
}
void OnImagePickerCancelled(object sender, EventArgs args)
{
taskCompletionSource.SetResult(null);
imagePicker.DismissModalViewController(true);
}
}
请分享此问题和错误的任何解决方案。