Download MonoTouch and monotouch
Application tried to present modally an active controller <UIImagePickerController: 0x7b6ff400>
1. AddGestureRecognizer adds an event to the image View
// Rounded Avatar _ 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; // if you do not set MasksToBounds to true, the rounded corner of the image is invalid _ avatarView. layer. cornerRadius = _ avatarRadius; _ avatarView. userInteractionEnabled = true; // user interaction attribute () _ avatarView. addGestureRecognizer (new UITapGestureRecognizer (onClickImage ));
Void onClickImage () {try {// UIActionSheet is a dialog box that appears in iOS. You can add multiple items and add click events for each item. // Override: override the Clicked method of UIActionSheetDelegate to obtain the currently Clicked item UIActionSheet _ actionSheet = new UIActionSheet ("select image", new ChangePictureActionSheetDelegate (this, _ avatarView), "cancel ", null, "from photo library", "photo"); _ actionSheet. showInView (this. view);} catch (Exception e) {Console. writeLine ("Error:" + e. message );}}
UIActionSheet is the selection button dialog box in iOS. You can add multiple items and add click events for each item.
2. Obtain the UIActionSheet option.
Key class: UIActionSheetDelegate. You can override the Clicked method of UIActionSheetDelegate to obtain the current click item of UIActionSheet.
Class authorization: UIActionSheetDelegate {UIViewController _ controller; UIImageView _ img; UIImagePickerController picker; public authorization (UIViewController controller, UIImageView img) {_ controller = controller; _ img = img ;} public override void Clicked (UIActionSheet actionSheet, int buttonIndex) {Console. writeLine ("You selected:" + 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; // allows you to edit the image picker. allowsEditing = true; if (_ controller. presentedViewController = null) {_ controller. presentModalViewController (picker, false);} // picker. finishedPickingImage + = picker_FinishedPickingImage; (invalid) // AvatarPickerDelegate: Obtain the picker of the selected image by rewriting the FinishedPickingImage method of UIImagePickerControllerDelegate. delegate = new AvatarPickerDelegate (_ img); Console. writeLine ("Finished");} catch (Exception e) {Console. writeLine ("Error:" + e. message) ;}} else {Console. writeLine ("this device does not support this operation ");}}}
3. Obtain the selected Image
Key class: UIImagePickerControllerDelegate. You can override the FinishedPickingImage method of UIImagePickerControllerDelegate to obtain the selected image.
Class preview: Upload {UIImageView _ avatar; public Preview (UIImageView _ imgView) {_ avatar = _ imgView;} public override void FinishedPickingImage (UIImagePickerController picker, UIImage image, NSDictionary editingInfo) {Console. writeLine ("selected image"); if (image = null) {Console. writeLine ("Null");} // update the display image _ avatar. layer. contents = image. CGImage; // disable UIImagePicker picker. dismissModalViewControllerAnimated (true); Console. writeLine ("finished selection ");}}