gpt4 book ai didi

c# - 从图库中选择照片样本在 iOS 10 上崩溃 #26

转载 作者:行者123 更新时间:2023-11-29 00:39:06 25 4
gpt4 key购买 nike

我已经尝试按照 Xamarin.ios 中关于如何在 IOS 中从图库中选择照片的教程进行操作。链接:https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/

但是,当我执行代码时没有错误,但是当我试图点击“选择器”按钮时它在我的模拟器上崩溃了。与这篇文章类似的问题: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) 删除对 base.ViewDidLoad();

的重复调用
public override void ViewDidLoad()
{
base.ViewDidLoad();
// base.ViewDidLoad();

2) 替换 PresentModalViewController,因为它在 iOS 6 中已被弃用:

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

3) 由于您正在访问用户的照片库,在 iOS10 中您必须请求许可,否则您的应用将崩溃并显示:

__CRASHING_DUE_TO_PRIVACY_VIOLATION__

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

  • 属性:隐私 - 照片库使用说明
  • 值:此应用需要访问您的照片库

enter image description here

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

enter image description here

关于c# - 从图库中选择照片样本在 iOS 10 上崩溃 #26,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39940511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com