菜鸟教程小白 发表于 2022-12-12 11:45:14

ios - 如何从 Xamarin.ios 中的图库中选择图像


                                            <p><p>我想从图库中加载图像并将其显示在 <code>UIImageView</code> 上。</p>

<p>我尝试像 Xamarin 食谱中的示例那样做,但我得到的只是空值。</p>

<p> <a href="https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/" rel="noreferrer noopener nofollow">https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/</a> </p>

<p>到目前为止我的代码:</p>

<pre><code>public partial class ViewController : UIViewController
    {
    UIImagePickerController imagePicker;
    UIButton choosePhotoButton;
UIImageView imageView;

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

public ViewController()
{
}

public override void ViewDidLoad()
{
    base.ViewDidLoad();
    Title = &#34;Wähle Bild aus:&#34;;
    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(&#34;Picker&#34;, UIControlState.Normal);
    choosePhotoButton.TouchUpInside += (s, e) =&gt;
    {
      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);

    };

    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.ToString())
    {
      case &#34;public.image&#34;:
            Console.WriteLine(&#34;Image selected&#34;);
            isImage = true;
            break;
      case &#34;public.video&#34;:
            Console.WriteLine(&#34;Video selected&#34;);
            break;
    }

    // get common info (shared between images and video)
    NSUrl referenceURL = e.Info as NSUrl;
    if (referenceURL != null)
      Console.WriteLine(&#34;Url:&#34; + referenceURL.ToString());

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

      UIImage editedImage = e.Info as UIImage;
      if (editedImage != null)
      {
            // do something with the image
            Console.WriteLine(&#34;got the edited image&#34;);
            imageView.Image = editedImage;
      }
    }

    // dismiss the picker
    imagePicker.DismissModalViewController(true);
}
</code></pre>

<p>我在这一行得到空值:
<code>NavigationController.PresentModalViewController(imagePicker, true);</code></p>

<p>我做错了什么? </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>看来你没有使用<code>UINavigationController</code>,所以改一下:</p>

<pre><code>NavigationController.PresentModalViewController(imagePicker, true);
</code></pre>

<p>收件人:</p>

<pre><code>PresentModalViewController(imagePicker, true);
</code></pre>

<p>注意:<code>PresentModalViewController</code> 自 ios6 起已弃用,因此您可以更改代码以使用:</p>

<pre><code>PresentViewControllerAsync(imagePicker, true);
</code></pre>

<p>或者:</p>

<pre><code>PresentViewController(imagePicker, true, () =&gt; {
});
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何从 Xamarin.ios 中的图库中选择图像,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/38476030/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/38476030/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何从 Xamarin.ios 中的图库中选择图像