UIImagePickerController显示最后点击的图像。我正在使用Monotouch / Xamarin Studio

时间:2013-11-19 21:43:00

标签: ios xamarin.ios xamarin

在我的项目中,当我第一次启动相机时它工作正常。当我第二次启动相机时,我看到最后一次点击取景器中的图像。我不确定是什么导致了这一点。

有人可以帮我吗?

以下是启动摄像头的代码块:

        UIImagePickerController imagePicker = new UIImagePickerController();
        // Handle media selected.
        var documentsDirectory = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
        imagePicker.FinishedPickingMedia += (sender, e) => {
            UIImage image = (UIImage)e.Info.ObjectForKey(
                new NSString("UIImagePickerControllerOriginalImage"));

            if (image != null)
            {
                this.InvokeOnMainThread(() => {
                    this.clickedImage.Image = image;
                    image.SaveToPhotosAlbum(delegate(UIImage img, NSError err){ 
                    });
                    string pngFilename = System.IO.Path.Combine (documentsDirectory, "Photo.png"); // hardcoded filename, overwrites each time
                    NSData imgData = image.AsPNG();
                    NSError SaveErr = null;
                    if (imgData.Save(pngFilename, false, out SaveErr))
                    {
                        Console.WriteLine("saved as " + pngFilename);
                    } else {
                        Console.WriteLine("NOT saved as" + pngFilename + " because" + SaveErr.LocalizedDescription);
                    }
                });
            }

            DismissViewController(true,null);

        };

        // Handle cancellation of picker.
        imagePicker.Canceled += (sender, e) => {
            DismissViewController(true,null);

        };



        btnCameraDisplay1.SetTitle("Take Picture", UIControlState.Normal);
        btnCameraDisplay1.Font = UIFont.SystemFontOfSize(19);
        btnCameraDisplay1.SetTitleColor(UIColor.Black, UIControlState.Normal);

        btnCameraDisplay1.TouchUpInside += delegate(object sender, EventArgs e)
        {
            if(UIImagePickerController.IsSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
            {
                imagePicker.SourceType = UIImagePickerControllerSourceType.Camera;
                imagePicker.AllowsEditing = false;
                this.PresentViewController(imagePicker, true,null);
            }
            else{
                alertView = new UIAlertView();
                alertView.AddButton("OK");
                alertView.Message = "No camera available in this device.";
                alertView.AlertViewStyle = UIAlertViewStyle.Default;
                alertView.Show();
            }
        };

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题。一些在Stackoverflow上搜索的人帮助:

解决方案: 从事件系统切换到代表,如下所述: https://stackoverflow.com/a/20035698/383658

所以基本上从以下地方移动你的代码:

imagePicker.FinishedPickingMedia += (sender, e) => {}

到你的新 UIImagePickerControllerDelegate 委托方法:

public override void FinishedPickingMedia (UIImagePickerController picker, NSDictionary info)
    {}