Hello Guys我正在开发一款应用程序,当你点击一个按钮时,它会打开iOS相机并让你拍照我知道如何在objc中执行此操作但使用Xamarin编写的应用程序使用Xamarin i&# 39;我看了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, () => {});
}
}
}
任何帮助都很棒
提前谢谢!
答案 0 :(得分:2)
你差不多完了它。只需为Picker添加委托处理程序,请查看: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);
}
答案 1 :(得分:1)
我的运行良好:
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;
}
}
}
方法" openCamera"通过.xib文件连接到Button事件。
答案 2 :(得分:0)
您可以使用XLabs:https://github.com/XLabs/Xamarin-Forms-Labs
XLabs提供可以拍照的相机服务:https://github.com/XLabs/Xamarin-Forms-Labs/wiki/Camera
如果您不熟悉XLabs,可以使用以下链接来帮助您入门。请记住将XLabs nuget包添加到您的所有项目(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
如何在XLabs中使用解析器:https://forums.xamarin.com/discussion/20178/xamarin-forms-labs-peeps
编辑:XLabs MediaPicker可以在Xamarin.Forms和非Xamarin.Forms应用程序中使用。