MonoTouch,monotouch下载

时间:2019-11-28 16:18来源:澳洲幸运10展示
MonoTouch,monotouch下载 Application tried to present modally an active controllerUIImagePickerController: 0x7b6ff400 1,AddGestureRecognizer为图片视图加多事件 //圆角头像 _avatarView = new UIImageView(new RectangleF(_blockS

MonoTouch,monotouch下载

Application tried to present modally an active controller <UIImagePickerController: 0x7b6ff400>

图片 1

1,AddGestureRecognizer为图片视图加多事件

 //圆角头像
 _avatarView = new UIImageView(new RectangleF(_blockSpace, _blockSpace, 2 * _avatarRadius, 2 * _avatarRadius));
UIImage img = UIImage.FromFile("profile-pic.jpg");
_avatarView.Layer.Contents = img.CGImage;

_avatarView.Layer.MasksToBounds = true;//如果没有将MasksToBounds设置为true,图片圆角无效
_avatarView.Layer.CornerRadius = _avatarRadius;

 _avatarView.UserInteractionEnabled = true;//用户互动属性()
_avatarView.AddGestureRecognizer(new UITapGestureRecognizer(onClickImage));

 

 void onClickImage()
 {
            try
            {
                //UIActionSheet是在iOS弹出的选择按钮项的对话框,可以添加多项,并为每项添加点击事件。
                //ChangePictureActionSheetDelegate:通过重写UIActionSheetDelegate的Clicked方法,获取当前点击项
                UIActionSheet _actionSheet = new UIActionSheet("选择图片",
                new ChangePictureActionSheetDelegate(this,_avatarView), "取消",
                null, "从相片库", "拍照");                
                _actionSheet.ShowInView(this.View);

            }
            catch (Exception e)
            {
                Console.WriteLine("Error:" + e.Message);
            }

 }

 UIActionSheet是在iOS弹出的拈轻怕重按钮项的对话框,能够加上多项,并为每项增多点击事件。 

 图片 2

2,获取UIActionSheet选择项

 关键类:UIActionSheetDelegate。通过重写UIActionSheetDelegate的Clicked方法,获取UIActionSheet当前点击项 

class ChangePictureActionSheetDelegate : UIActionSheetDelegate
    {
        UIViewController _controller;
        UIImageView _img;
        UIImagePickerController picker;
        public ChangePictureActionSheetDelegate(UIViewController controller,UIImageView img)
        {
            _controller = controller;
            _img = img;
        }
        public override void Clicked(UIActionSheet actionSheet, int buttonIndex)
        {
            Console.WriteLine("你选择了:" + buttonIndex);
            switch (buttonIndex)
            {
                case 1: CallPhoto(UIImagePickerControllerSourceType.Camera); break;
                case 0: CallPhoto(UIImagePickerControllerSourceType.PhotoLibrary); break;
                default: break;
            }
        }        

        private void CallPhoto(UIImagePickerControllerSourceType type)
        {
            Console.WriteLine("Begin...");
            if (UIImagePickerController.IsSourceTypeAvailable(type))
            {
                try
                {
                    picker = new UIImagePickerController();
                    picker.SourceType = type;
                    //允许编辑图片
                    picker.AllowsEditing = true;
                    if (_controller.PresentedViewController == null)
                    {
                        _controller.PresentModalViewController(picker, false);
                    }
                    //picker.FinishedPickingImage += picker_FinishedPickingImage;(无效)
                    //AvatarPickerDelegate:通过重写UIImagePickerControllerDelegate的FinishedPickingImage方法获取选择的图片
                    picker.Delegate =new AvatarPickerDelegate(_img);
                    Console.WriteLine("Finished");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error:" + e.Message);
                }
            }
            else
            {
                Console.WriteLine("该设备不支持此操作");
            }
        }


    }

 图片 3图片 4图片 5

3,获取选取的图样

要害类:UIImagePickerControllerDelegate。通过重写UIImagePickerControllerDelegate的FinishedPickingImage方法获得选拔的图样

class AvatarPickerDelegate:UIImagePickerControllerDelegate {

        UIImageView _avatar;
        public AvatarPickerDelegate(UIImageView _imgView) {
            _avatar = _imgView;
        }        
        public override void FinishedPickingImage(UIImagePickerController picker, UIImage image, NSDictionary editingInfo)
        {


            Console.WriteLine("选择了图片");
            if (image == null)
            {
                Console.WriteLine("Null");
            }
           //更新显示图片
            _avatar.Layer.Contents = image.CGImage;
            //关闭UIImagePicker
            picker.DismissModalViewControllerAnimated(true);
            Console.WriteLine("完成选择");

        }
    }

  图片 6

Application tried to present modally an active controller UIImagePickerController: 0x7b6ff400 1,AddGestureRecognizer为图片视图增多事件 //圆角...

编辑:澳洲幸运10展示 本文来源:MonoTouch,monotouch下载

关键词: