Application tried to present modally an active controller <UIImagePickerController: 0x7b6ff400>
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,获取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,获取选择的图片
关键类: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("完成选择"); } }
时间: 2024-10-14 01:16:03