简体   繁体   中英

How to take a picture with iOS camera and save the image to camera roll using Xamarin

Hello Guys i'm working on a app that when you click a button it opens the stock iOS camera and lets you take a picture I know how to do this in objc but the apps written in C# using Xamarin i've looked on the Xamarin forms and google for help but everything is post iOS 8 witch this app needs to run so here's the code that I have so far:

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

        };

EDIT I ADDED THE Following code to a new class:

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, () => {});
        }


    }
}

Any Help would be awesome

Thank you in advance!

You've almost finished it. Just add Delegate handler for Picker, take a look on this: https://developer.xamarin.com/recipes/ios/media/video_and_photos/choose_a_photo_from_the_gallery/

I added events below follow your existing source code

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);
}

Im running good with this:

public partial class CameraViewController : UIViewController
{
    static UIImagePickerController picker;
    static UIImageView staticImageView;

    public CameraViewController() : base("CameraViewController", null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        base.Title = "Kamera";
        staticImageView = this.imageView;
    }

    partial void openCamera(UIButton sender)
    {
        if (UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
        {
            picker = new UIImagePickerController();
            picker.Delegate = new CameraDelegate();
            picker.SourceType = UIImagePickerControllerSourceType.Camera;
            NavigationController.PresentViewController(picker, true, null);
        }
        else
        {
            this.button.Hidden = true;
        }


    }

    class CameraDelegate : UIImagePickerControllerDelegate
    {
        public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
        {
            picker.DismissModalViewController(true);
            var image = info.ValueForKey(new NSString("UIImagePickerControllerOriginalImage")) as UIImage;
            CameraViewController.staticImageView.Image = image;
        }
    }

}

The method "openCamera" is connected to the Button Event via .xib file.

You can use XLabs: https://github.com/XLabs/Xamarin-Forms-Labs

XLabs provides the Camera service which can take a picture: https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera

If you are not familiar with XLabs, these are a few links which help you getting started. Please remember to add the XLabs nuget package to all of your projects (PCL, iOS, Android) http://www.codenutz.com/getting-started-xamarin-forms-labs-xaml-mvvm-ioc/ https://github.com/XLabs/Xamarin-Forms-Labs/tree/master/Samples

How to use Resolver in XLabs: https://forums.xamarin.com/discussion/20178/xamarin-forms-labs-peeps

EDIT: XLabs MediaPicker can be used in both Xamarin.Forms and non-Xamarin.Forms app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM