UIImagePickerController没有显示在xamarin iOS

时间:2016-05-29 14:24:40

标签: ios xamarin xamarin.ios

由于一些奇怪的原因,我的UIImagePickerController没有显示运行此代码?你能看到问题吗?

这是在我的iPhone 5上 - 我只是出现黑屏 - 我无法进行任何互动

var pictureViewer = new PictureViewController((APTask)GetItem(e.IndexPath.Row), e.IndexPath);
parent.NavigationController.PresentViewController(pictureViewer, true, null);

这是图片classview类:

public class PictureViewController : UIViewController, IUINavigationControllerDelegate
{
    private UIImagePickerController _imagePicker;
    private APTask _task;
    private NSIndexPath _indexPath;
    private UIImage _image;
    private NSDictionary _imageMetadata;

    public PictureViewController(APTask task, NSIndexPath indexPath)
    {
        this._task = task;
        this._indexPath = indexPath;
    }


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

        _imagePicker = new UIImagePickerController ();

        // set our source to the photo library
        _imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;

        _imagePicker.ModalPresentationStyle = UIModalPresentationStyle.CurrentContext;
        // set what media types
//      _imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes (UIImagePickerControllerSourceType.PhotoLibrary);

        _imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia;
        _imagePicker.Canceled += Handle_Canceled;

        _imagePicker.Delegate = this;

        PresentViewController(_imagePicker, true, null);

    }

1 个答案:

答案 0 :(得分:1)

您需要在展示UIImagePickerController的竞赛中展示pictureViewer,因为它是UIViewController

您的代码:

parent.NavigationController.PresentViewController(pictureViewer, true, null);

不需要为竞争处理程序传递null。您可以在PictureViewController类上公开一个公共方法来创建/显示UIImagePickerController并将Action(方法)作为处理程序传递...

但是为什么创建/呈现一个空的UIViewController只是用UIImagePickerController覆盖它,我就是这样做的...

注意:此代码还可以正确处理iPad演示文稿,如果您在模拟器中请求相机,则默认为PhotoLibrary以避免本机ObjC异常。

public partial class myViewController : UIViewController
{
    UIImagePickerController _imagePickerController;

    public myViewController (IntPtr handle) : base (handle) { }

    partial void myButtonTouch(UIButton sender)
    {
        ImagePickerController(UIImagePickerControllerSourceType.Camera);
    }

    public void ImagePickerController(UIImagePickerControllerSourceType sourceType)
    {
        if (_imagePickerController == null)
            _imagePickerController = new UIImagePickerController();
        if (Runtime.Arch == Arch.DEVICE) // No camara on Simulator
            _imagePickerController.SourceType = sourceType;
        else
            if (sourceType == UIImagePickerControllerSourceType.Camera)
                _imagePickerController.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        if (UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad) // Handle ipad correctly 
        {
            if (_imagePickerController.SourceType == UIImagePickerControllerSourceType.Camera)
                _imagePickerController.ModalPresentationStyle = UIModalPresentationStyle.FullScreen;
            else
                _imagePickerController.ModalPresentationStyle = UIModalPresentationStyle.Popover;
        }
        else
        {
            _imagePickerController.ModalPresentationStyle = UIModalPresentationStyle.CurrentContext;
        }

        _imagePickerController.Canceled += (object sender, EventArgs e) =>
        {
            Console.WriteLine("Picker Cancelled");
            _imagePickerController.DismissViewController(true, null);
        };
        _imagePickerController.FinishedPickingMedia += (object sender, UIImagePickerMediaPickedEventArgs e) =>
        {
            _imagePickerController.DismissViewController(true, null);
            Console.WriteLine(e.ReferenceUrl);
            if (_imagePickerController.SourceType == UIImagePickerControllerSourceType.Camera)
            {
                // Newly-captured media, save it to the Camera Roll on the device or ....
            }
            else
            {
                // Existing media seleted, do something with it....
            }
        };

        var mainWindow = UIApplication.SharedApplication.KeyWindow;
        var viewController = mainWindow?.RootViewController;
        while (viewController?.PresentedViewController != null)
        {
            viewController = viewController.PresentedViewController;
        }
        if (viewController == null)
            viewController = this;
        _imagePickerController.View.Frame = viewController.View.Frame;
        viewController.PresentViewController(_imagePickerController, true, () => { Console.WriteLine("Complete"); });
    }
}

注意:您可以提取ImagePickerController方法并将其作为静态方法添加到某处以便重用,因为while (viewController?.PresentedViewController != null)代码段始终会找到正确的上下文来呈现模态控制器