Skip to main content
 首页 » 编程设计

c#之如何使用 iOS 相机拍照并使用 Xamarin 将图像保存到相机胶卷

2024年05月29日23davidwang456

大家好,我正在开发一个应用程序,当您单击一个按钮时,它会打开 iOS 相机并让您拍照 我知道如何在 objc 中执行此操作,但我看过使用 Xamarin 用 C# 编写的应用程序在 Xamarin 表单和谷歌上寻求帮助,但一切都在 iOS 8 之后,这个应用程序需要运行所以这是我到目前为止的代码:

photobutton.TouchUpInside += delegate { 
//insert Xamarin code here 
 
        }; 

编辑我将以下代码添加到新类中:

using Foundation; 
using System; 
using System.CodeDom.Compiler; 
using UIKit; 
 
namespace ToolBelt.iOS 
{ 
    public partial class cameraViewController : UIViewController 
    { 
        public cameraViewController (IntPtr handle) : base (handle) 
        {        
        } 
 
 
 
        public override void DidReceiveMemoryWarning () 
        { 
            // Releases the view if it doesn't have a superview. 
            base.DidReceiveMemoryWarning (); 
 
            // Release any cached data, images, etc that aren't in use. 
        } 
 
        public override void ViewDidLoad () 
        { 
            base.ViewDidLoad (); 
 
 
            //UIPopoverController popover = new UIPopoverController (ctrl); 
 
            // Perform any additional setup after loading the view, typically from a nib. 
        } 
 
        public override void ViewDidAppear (bool animated) 
        { 
            base.ViewDidAppear (animated); 
 
            UIImagePickerController picker = new UIImagePickerController ();picker.PrefersStatusBarHidden (); 
 
            picker.SourceType = UIImagePickerControllerSourceType.Camera; 
            PresentViewController(picker, true, () => {}); 
        } 
 
 
    } 
} 

任何帮助都会很棒

提前致谢!

请您参考如下方法:

你快完成了。只需为 Picker 添加委托(delegate)处理程序,看看这个:https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/

我在下面添加了事件,遵循您现有的源代码

UIImagePickerController imagePicker; 
 
public override void ViewDidAppear (bool animated) 
{ 
    base.ViewDidAppear (animated); 
 
    imagePicker = new UIImagePickerController(); 
    imagePicker.PrefersStatusBarHidden (); 
 
    imagePicker.SourceType = UIImagePickerControllerSourceType.Camera; 
 
    //Add event handlers when user finished Capturing image or Cancel 
    imagePicker.FinishedPickingMedia += Handle_FinishedPickingMedia; 
    imagePicker.Canceled += Handle_Canceled; 
 
    //present  
    PresentViewController(picker, true, () => {}); 
} 
 
protected void Handle_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"); 
            imageView.Image = originalImage; // display 
        } 
    } 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.DismissModalViewControllerAnimated (true); 
} 
 
void Handle_Canceled (object sender, EventArgs e) { 
    imagePicker.DismissModalViewControllerAnimated(true); 
}