从iOS 10#26

时间:2016-10-09 05:58:25

标签: c# ios xamarin xamarin.ios

我尝试过遵循Xamarin.ios的教程,了解如何在IOS中从gallary中选择照片。链接:https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/

然而,当我实现代码时有没有错误但是当我试图点击" picker"时它在我的模拟器上崩溃了。按钮。与此帖相似的问题:https://github.com/xamarin/recipes/issues/26

任何人都可以帮我吗??? 以下是我的代码,来自Xamarin。

using System;
using CoreGraphics;
using AssetsLibrary;
using UIKit;
using Foundation;

namespace App1
{
    public partial class ImageViewController : UIViewController
    {

    UIImagePickerController imagePicker;
    UIButton choosePhotoButton;
    UIImageView imageView;

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

    public ImageViewController()
    {

    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        base.ViewDidLoad();
        Title = "Wähle Bild aus:";
        View.BackgroundColor = UIColor.White;

        imageView = new UIImageView(new CGRect(10, 150, 300, 300));
        Add(imageView);

        choosePhotoButton = UIButton.FromType(UIButtonType.RoundedRect);
        choosePhotoButton.Frame = new CGRect(10, 80, 100, 40);
        choosePhotoButton.SetTitle("Picker", UIControlState.Normal);
        choosePhotoButton.TouchUpInside += (s, e) =>
        {
            imagePicker = new UIImagePickerController();

            imagePicker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            imagePicker.MediaTypes = UIImagePickerController.AvailableMediaTypes(UIImagePickerControllerSourceType.PhotoLibrary);

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

            //NavigationController.PresentModalViewController(imagePicker, true);
            PresentModalViewController(imagePicker, true);

        };

        View.Add(choosePhotoButton);

    }


    private void Handle_Canceled(object sender, EventArgs e)
    {
        imagePicker.DismissModalViewController(true);
    }

    protected void Handle_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
    {
        bool isImage = false;
        switch (e.Info[UIImagePickerController.MediaType].ToString())
        {
            case "public.image":
                Console.WriteLine("Image selected");
                isImage = true;
                break;
            case "public.video":
                Console.WriteLine("Video selected");
                break;
        }

        // get common info (shared between images and video)
        NSUrl referenceURL = e.Info[new NSString("UIImagePickerControllerReferenceUrl")] as NSUrl;
        if (referenceURL != null)
            Console.WriteLine("Url:" + referenceURL.ToString());

        // if it was an image, get the other image info
        if (isImage)
        {
            // get the original image
            UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
            if (originalImage != null)
            {
                // do something with the image
                imageView.Image = originalImage; // display
            }

            UIImage editedImage = e.Info[UIImagePickerController.EditedImage] as UIImage;
            if (editedImage != null)
            {
                // do something with the image
                Console.WriteLine("got the edited image");
                imageView.Image = editedImage;
            }
        }

        // dismiss the picker
        imagePicker.DismissModalViewController(true);
    }
}
}

1 个答案:

答案 0 :(得分:1)

1)删除对base.ViewDidLoad();

的重复通话
public override void ViewDidLoad()
{
    base.ViewDidLoad();
    // base.ViewDidLoad();

2)替换在iOS 6中弃用的PresentModalViewController

    //PresentModalViewController(imagePicker, true);
    PresentViewController(imagePicker, true, null);

3)由于您正在访问用户的照片库,因此在 iOS10 中,您必须要求获得许可,否则您的应用将会崩溃:

__CRASHING_DUE_TO_PRIVACY_VIOLATION__

在您应用的info.plist中添加字符串条目:

  • 财产:Privacy - Photo Library Usage Description
  • 价值:This app needs to access your photo library

enter image description here

这将导致系统提示用户接受或拒绝您的应用访问

enter image description here