如何在Xamarin Native iOS中访问相机和图库,并将图像路径传递给下一个视图控制器

问题描述 投票:0回答:1

我想制作一个可以拍照的iOS应用程序,还可以从相机胶卷中选择图片。我想要实现一个像like this这样的应用,但是,我遇到了问题。

1. I can't get the image path from the gallery
2. I can't access my camera. Every time I try to open the camera it crashes. 

当我单击图库中的图片时,您可以从我的图片中看到它,它没有显示所选的图片,但仍然空白。而且,当我单击“照相机”按钮时,它应该显示带有快门按钮的照相机屏幕,而当我单击“快门”按钮时,它将参考下一页。但是,当我启动相机时,它崩溃了。

但是到目前为止,我已经尝试制作该应用程序并编写一些代码。请任何人可以帮助我吗?如果有人可以帮助我,我会很高兴,因为我对iOS还是很陌生。我使用C#在Xamarin iOS Native中编写代码。这是我的代码

UIImagePickerController imagePicker;
        public UploadTaskViewController (IntPtr handle) : base (handle)
        {
        }

        string fileName;
        string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        int Count = 0;

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

            uploadPage_btnCamera.TouchUpInside += UploadPage_btnCamera_TouchUpInside;
            uploadPage_btnGallery.TouchUpInside += UploadPage_btnGallery_TouchUpInside;
        }

        public override void ViewDidAppear(bool animated)
        {
            base.ViewDidAppear(animated);

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

        private void ImagePicker_FinishedPickingMedia(object sender, UIImagePickerMediaPickedEventArgs e)
        {
            // determine what was selected, video or image
            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
                    Console.WriteLine("got the original image");
                    //.Image = originalImage; // display
                    //StoreImage(referenceURL.ToString());
                }
            }
            else
            { // if it's a video
              // get video url
                NSUrl mediaURL = e.Info[UIImagePickerController.MediaURL] as NSUrl;
                if (mediaURL != null)
                {
                    Console.WriteLine(mediaURL.ToString());
                }
            }
            // dismiss the picker
            imagePicker.DismissModalViewController(true);
        }

        //public void StoreImage(string filename)
        //{
        //    if (File.Exists(fileName))
        //    {
        //        Count++;
        //        string filenames = "image" + Count.ToString() + ".jpg";
        //        fileName = Path.Combine(folderPath, filenames);
        //    }
        //    else
        //    {
        //        fileName = Path.Combine(folderPath, "image.jpg");
        //    }
        //    var img = UIImage.FromFile(filename);
        //    NSData image = img.AsJPEG();
        //    NSError err = null;
        //    image.Save(fileName, false, out err);
        //    PassedVariablesClass.m_imageString = fileName;
        //    //imgProfile.Image = UIImage.FromFile(fileName);
        //}

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

        private void UploadPage_btnCamera_TouchUpInside(object sender, EventArgs e)
        {
            //UIImagePickerController picker = new UIImagePickerController();
            //picker.PrefersStatusBarHidden();
            //picker.SourceType = UIImagePickerControllerSourceType.Camera;
            //PresentViewController(picker, true, () => { });

            //imagePicker = new UIImagePickerController();
            //imagePicker.PrefersStatusBarHidden();

            //imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;

            ////present 
            //PresentViewController(imagePicker, true, () => { });
        }

        private void UploadPage_btnGallery_TouchUpInside(object sender, EventArgs e)
        {
            imagePicker.FinishedPickingMedia += ImagePicker_FinishedPickingMedia;
            imagePicker.Canceled += ImagePicker_Canceled;
            NavigationController.PresentModalViewController(imagePicker, true);
        }
    }
}

我做了一些功能,例如StoreImage(string filename),我尝试捕获所选的图像,然后尝试放置图像路径并将其发送到下一页。我将带有PassedVariablesClass.m_imageString = fileName;的图像路径传递到下一页。

我错过了什么?有人可以告诉我吗?预先感谢您的帮助!

c# ios xamarin xamarin.ios
1个回答
0
投票

首先,请确保您已经在Info.plist中添加了用户权限。

<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app needs access to microphone.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>This app needs access to the photo gallery.</string>

并且在您的项目中

  if (isImage)
        {
            // get the original image
            UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;

            string filePath = e.ImageUrl.ToString();

            if (originalImage != null)
            {

                label.Text = filePath;


                imageView.Image = originalImage;
            }
        }

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.