自定义相机渲染器无法在UIImagePickerControllerDelegate

时间:2018-08-10 14:35:30

标签: xamarin.forms xamarin.ios

下面的代码不起作用,因为有回叫不能 捕获图像后发生的情况。只是无限 环。在我的应用程序中,我有一个用于相机的自定义渲染器,并且有一个按钮。单击按钮后,我要保存相机中的图像。点击后 按钮,我已经实现了一个接口,并且有一种叫做Takepicture的方法,实际上我想从相机中保存图像。 android版本对我来说工作正常,但是当我实施时:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

using Foundation;
using UIKit;

namespace Janus.iOS.Helpers
{
    public class Camera
    {
        static UIImagePickerController picker;
        static Action<NSDictionary> _callback;
        static void Init()
        {
            if (picker != null)
                return;

            picker = new UIImagePickerController { SourceType = UIImagePickerControllerSourceType.Camera }; ;
            picker.FinishedPickingImage += callback();
            picker.Delegate = new CameraDelegate();
        }

        private static EventHandler<UIImagePickerImagePickedEventArgs> callback()
        {
            return (sender, ev) =>
            {
                var filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tmp.png");
                UIImage image = (UIImage)ev.Image;// ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
                //picker.Dismi(true);
                //var image = (UIImage)ev.Info.ObjectForKey(new NSString("UIImagePickerControllerOriginalImage"));
            };
        }

        public class CameraDelegate : UIImagePickerControllerDelegate
        {
            public override void FinishedPickingMedia(UIImagePickerController picker, NSDictionary info)
            {
                var originalImg = new NSString("UIImagePickerControllerOriginalImage");
                var image = (UIImage)info[originalImg];
                var imageView = new UIImageView(new RectangleF()); // fill this in!
                imageView.Image = image;
                var cb = _callback;
                _callback = null;

                picker.DismissViewController(true, (Action)null);
                cb(info);
            }
            [Foundation.Export("imagePickerController:didFinishPickingImage:editingInfo:")]
            public override void FinishedPickingImage(UIKit.UIImagePickerController picker, UIKit.UIImage image, Foundation.NSDictionary editingInfo)
            {
                var filepath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "tmp.png");
                //throw new NotImplementedException();
            }

            public override void Canceled(UIImagePickerController picker)
            {
                var cb = _callback;
                _callback = null;

                picker.DismissViewController(true, (Action)null);
                cb(null);
            }
        }
        public static void TakePicture(UIViewController parent, Action<NSDictionary> callback)
        {
            Init();
            picker.SourceType = UIImagePickerControllerSourceType.Camera;
            _callback = callback;
            parent.PresentViewController((UIViewController)picker, true, (Action)null);
        }

        public static void SelectPicture(UIViewController parent, Action<NSDictionary> callback)
        {
            Init();
            picker.SourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            _callback = callback;
            parent.PresentViewController((UIViewController)picker, true, (Action)null);
        }
    }
}

?????

0 个答案:

没有答案