Download MonoTouch and monotouch

Source: Internet
Author: User

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 ");}}

  



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.