1. First of all, three agreements must be followed.
Uiactionsheetdelegate,uinavigationcontrollerdelegate,uiimagepickercontrollerdelegate
2. Main code
Select Avatar-(ibaction) Handleselectimage: (ID) Sender {self.myactionsheet = [[Uiactionsheet alloc] Initwit Htitle:nil delegate:self cancelbuttontitle:@ "Cancel" Destructivebutto Ntitle:nil otherbuttontitles:@ "Open camera" @ "get from album", Nil]; [Self.myactionsheet ShowInView:self.view];} #pragma mark-uiactionsheetdelegate-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (NSInteger ) buttonindex{if (Buttonindex = = Self.myActionSheet.cancelButtonIndex) {NSLog (@ "you canceled"); } switch (buttonindex) {case 0: [Self takephoto]; Break Case 1: [Self localphoto]; Break Default:break; }}//Photography-(void) takephoto{uiimagepickercontrollersourcetype sourcetype = Uiimagepickercontrollersourcetypecamera; if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {UIIMagepickercontroller *picker = [[Uiimagepickercontroller alloc] init]; Picker.delegate = self; Set the picture after the photo can be edited picker.allowsediting = YES; Picker.sourcetype = sourcetype; [Self Presentviewcontroller:picker animated:yes completion:nil]; }else {NSLog (@) simulates the camera cannot be turned on, please use it in the real machine); }}//Select Local picture-(void) localphoto{uiimagepickercontroller *picker = [[Uiimagepickercontroller alloc] init]; Picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary; Picker.delegate = self; picker.allowsediting = YES; [Self Presentviewcontroller:picker animated:yes completion:nil];} When a picture is selected, enter here-(void) Imagepickercontroller: (uiimagepickercontroller*) Picker Didfinishpickingmediawithinfo: ( Nsdictionary *) info{nsstring *type = [info objectforkey:uiimagepickercontrollermediatype]; When the selected type is picture if ([Type isequaltostring:@ "Public.image"]) {//First turn the picture into nsdata uiimage* image = [Info object forkey:@ "UiimagepickercontrollerorigiNalimage "]; NSData *data; if (uiimagepngrepresentation (image) = = nil) {data = UIImageJPEGRepresentation (image, 1.0); } else {data = uiimagepngrepresentation (image); }//Picture saved path//Here the picture is placed in the sandbox's Documents folder NSString * Documentspath = [Nshomedirectory () Stringbya ppendingpathcomponent:@ "Documents"]; File Manager Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; Copy the data object that you just converted into a sandbox and save it as Image.png [FileManager createdirectoryatpath:documentspath withintermediatedirectories: YES Attributes:nil Error:nil]; [FileManager createfileatpath:[documentspath stringbyappendingstring:@ "/image.png"] contents:data Attributes:nil]; Get the full path of the picture in the sandbox after the selection self.filepath = [[NSString alloc]initwithformat:@ "%@%@", Documentspath, @ "/image.png"]; Close the album interface [Picker Dismissviewcontrolleranimated:yes Completion:nil]; Create a selection after the small icon of the picture placed below//similar to the effect of a meager selection chart uiimageview *smallimage = [[Uiimageview alloc] initWithFrame: CGRectMake (110, 390, 75, 40)]; Smallimage.image = image; Add in view [self.imageselectbtn removefromsuperview];//remove button [Self.view addsubview:smallimage]; } }
<pre name= "code" class= "OBJC" >-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker{ NSLog (@ "You have canceled the selection of pictures"); [Picker Dismissviewcontrolleranimated:yes completion:nil];}
Transfer the system camera or get the picture from your local photo album.