The functions are as follows:
1. Click on the avatar to select the change Avatar Mode ① album ② Photography.
2. Click on the album to achieve the replacement by reading the system album and getting the picture.
3. Click on the camera to take pictures and replace the photos.
4. If the camera, pop-up box warning.
The code is as follows:
1. Prompt function via Uiactionsheet object
Create Object Uiactionsheet *actionsheet = [[Uiactionsheet alloc] Initwithtitle: @ "hint" delegate:self cancelbuttontitle:@ "Cancel" Destructivebuttontitle:nil otherbuttontitles:@ "album", @ "photo", nil]; Show [Actionsheet ShowInView:self.view] on the view; [Actionsheet release];
2. Implement the corresponding agent event, Agent Uiactionsheetdelegate, the method is as follows
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex { //album 0 photo 1 switch (buttonindex) {case 0: //Read from the album [self readimagefromalbum]; break; Case 1: //Take photos [self readimagefromcamera]; break; Default: Break ; }}
3. Realize the function of reading pictures from the album, the code is as follows
Read from album-(void) Readimagefromalbum { //Create object uiimagepickercontroller *imagepicker = [[ Uiimagepickercontroller alloc] init]; (select type) indicates that only photos are selected from the album imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary; Specify the proxy, so we want to implement Uiimagepickercontrollerdelegate, uinavigationcontrollerdelegate protocol imagepicker.delegate = Self; Sets whether to skip to edit mode for picture clipping after the album has finished selecting photos. (Allow user to edit) imagepicker.allowsediting = YES; Show albums [self Presentviewcontroller:imagepicker animated:yes completion:nil]; [Imagepicker release]; }
4. Achieve photo function
-(void) Readimagefromcamera { if ([Uiimagepickercontroller issourcetypeavailable: Uiimagepickercontrollersourcetypecamera]) { Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller ALLOC] init]; Imagepicker.sourcetype = Uiimagepickercontrollersourcetypecamera; Imagepicker.delegate = self; imagepicker.allowsediting = YES; Allow user to edit [self presentviewcontroller:imagepicker animated:yes completion:nil]; [Imagepicker release]; } else { //Popup Response Click event Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Warning" message:@ "Camera not detected" Delegate:nil cancelbuttontitle:nil otherbuttontitles:@ "OK", nil]; [Alert show]; [Alert release];} }
5. The picture completes the processing to submit, the Proxy method uipickercontrollerdelegate
After the picture is finished processing-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) Image editinginfo: (nsdictionary *) Editinginfo { //image is the modified photo //Add the picture to the corresponding view [button setimage:image Forstate:uicontrolstatenormal]; End Operation [self Dismissviewcontrolleranimated:yes completion:nil];}
"Learn iOS: UI Series" Click the change avatar to read photos and take pictures from the album two functions