Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Presenting UIImagePickerController crashes app

I am trying to present a UIImagePickerController via a Button action. My project crashes with error:

Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application.

I only have a ViewController embedded in a NavigationController in the storyboard. Code snippets below:

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);
}
like image 725
DrPatience Avatar asked Sep 19 '16 09:09

DrPatience


2 Answers

Added Privacy - Camera Usage Description to your info.plist and that resolved the issue

like image 160
DrPatience Avatar answered Oct 05 '22 16:10

DrPatience


Add the following to your info.plist for camera use:

<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>
like image 22
tomW Avatar answered Oct 05 '22 17:10

tomW