Application tried to present modally an active controller <UIImagePickerController:0x7b6ff400>
1,addgesturerecognizer adding an event to a picture view
Fillet 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 the maskstobounds is not set to true, the picture rounded corners are not valid _avatarview.layer.cornerradius = _avatarradius; _avatarview.userinteractionenabled = true;//User interaction attribute () _avatarview.addgesturerecognizer (New UITapGestureRecognizer ( Onclickimage));
void Onclickimage () { try { //uiactionsheet is a dialog box that pops up the Select button item in iOS, you can add multiple items and add a click event for each item. //changepictureactionsheetdelegate: Gets the current click by overriding the clicked method of the Uiactionsheetdelegate Uiactionsheet _actionsheet = new Uiactionsheet ("Select Picture", new Changepictureactionsheetdelegate (This,_avatarview), " Cancel ", null," from Photo Gallery "," take photos "); _actionsheet.showinview (this. View); } catch (Exception e) { Console.WriteLine ("Error:" + e.message);} }
Uiactionsheet is a dialog box for selecting button items that pops up in iOS, you can add multiple items and add click events for each item.
2, get Uiactionsheet selection
key class: Uiactionsheetdelegate. By overriding the clicked method of the Uiactionsheetdelegate, get the Uiactionsheet current click item
Class Changepictureactionsheetdelegate:uiactionsheetdelegate {Uiviewcontroller _controller; Uiimageview _img; Uiimagepickercontroller Picker; Public changepictureactionsheetdelegate (Uiviewcontroller controller,uiimageview img) {_controller = con Troller; _img = img; } public override void Clicked (Uiactionsheet actionsheet, int buttonindex) {Console.WriteLine ("You Selected: "+ Buttonindex); Switch (buttonindex) {case 1:callphoto (Uiimagepickercontrollersourcetype.camera); Case 0:callphoto (uiimagepickercontrollersourcetype.photolibrary); Break Default:break; }} private void Callphoto (Uiimagepickercontrollersourcetype type) {Conso Le. WriteLine ("Begin ..."); if (uiimagepickercontroller.issourcetypeavailable (type)) {try {picker = new Uiimagepickercontroller (); Picker. SourceType = type; Allows editing of picture picker. Allowsediting = true; if (_controller. Presentedviewcontroller = = null) {_controller. Presentmodalviewcontroller (picker, false); }//picker. Finishedpickingimage + = Picker_finishedpickingimage; (invalid)//avatarpickerdelegate: by overriding Uiimagepickercontro Llerdelegate's Finishedpickingimage method gets the selected picture picker. Delegate =new avatarpickerdelegate (_img); Console.WriteLine ("Finished"); } catch (Exception e) {Console.WriteLine ("Error:" + e.message); }} else {Console.WriteLine ("The device does not support this operation"); } } }
3. Get the selected picture
Key class: Uiimagepickercontrollerdelegate. Get the selected picture by overriding the Uiimagepickercontrollerdelegate Finishedpickingimage method
Class Avatarpickerdelegate:uiimagepickercontrollerdelegate { Uiimageview _avatar; Public avatarpickerdelegate (Uiimageview _imgview) { _avatar = _imgview; } public override void Finishedpickingimage (Uiimagepickercontroller picker, UIImage image, Nsdictionary editinginfo) { Console.WriteLine ("Image selected"); if (image = = null) { Console.WriteLine ("null"); } Update the display picture _avatar. Layer.contents = image. Cgimage; Close the Uiimagepicker picker. Dismissmodalviewcontrolleranimated (true); Console.WriteLine ("Finish Selection"); } }
Monotouch-ios use Uiimagepickercontroller to open Picture Library and camera Select picture to modify Avatar