呈现UIImagePickerController会崩溃应用程序

时间:2016-09-19 09:21:46

标签: ios xamarin xamarin.ios uiimagepickercontroller

我试图通过Button动作呈现UIImagePickerController。我的项目因错误而崩溃:

  

执行本机代码时获得了SIGABRT。这通常表明   单声道运行时或其中一个本机库中的致命错误   由您的应用程序使用。

我只在Storyboard中的NavigationController中嵌入了一个ViewController。下面的代码片段:

UIImagePickerController imagePicker;

public override void ViewDidLoad()
{
    base.ViewDidLoad();

    this.setupImagePicker();

    CapturePhotoButton.TouchUpInside += delegate
    {
        this.AddMedia();
    };
}

public void setupImagePicker()
{
    imagePicker = new UIImagePickerController();
    imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
    imagePicker.ModalPresentationStyle = UIModalPresentationStyle.Popover;
    imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(
        UIImagePickerControllerSourceType.Camera);
    imagePicker.FinishedPickingMedia += HandleFinishedPickingMedia;
    imagePicker.Canceled += (sender, e) => {
        imagePicker.DismissModalViewController(true);
    };
}

public void HandleFinishedPickingMedia(object sender, 
    UIImagePickerMediaPickedEventArgs e)
{
    bool isImage = false;

    switch (e.Info[UIImagePickerController.MediaType].ToString())
    {
        case "public.image":
            isImage = true;
            break;
        case "public.video":
            break;
    }

    if (isImage)
    {
        UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
        if (originalImage != null)
        {
            PreviewImageView.Image = originalImage;
            imagePicker.DismissViewController(true, null);
        }
    }
}

public void AddMedia()
{
    //Crashes on this line
    this.NavigationController.PresentViewController(imagePicker, true, null);
}

2 个答案:

答案 0 :(得分:3)

为您的info.plist添加了隐私 - 相机使用说明,解决了问题

答案 1 :(得分:2)

将以下内容添加到info.plist以供相机使用:

<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>

for library:

<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos.</string>