如何使用 iOS 相机拍照并使用 Xamarin 将图像保存到相机胶卷 [英] How to take a picture with iOS camera and save the image to camera roll using Xamarin

查看:38
本文介绍了如何使用 iOS 相机拍照并使用 Xamarin 将图像保存到相机胶卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


    }
}

任何帮助都会很棒

先谢谢你!

推荐答案

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

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

这篇关于如何使用 iOS 相机拍照并使用 Xamarin 将图像保存到相机胶卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆