1.
Uiactionsheet *actionsheet = [[Uiactionsheet alloc] Initwithtitle: @ "set Avatar" Delegate:self can celbuttontitle:@ "Cancel" destructivebuttontitle:nil otherbuttontitles:@ "select local image" @ "photo", nil];
[Actionsheet ShowInView:self.view];
2. Implement the corresponding agent event, Agent Uiactionsheetdelegate, the method is as follows
-(void) Actionsheet: (Uiactionsheet *) Actionsheet Clickedbuttonatindex: (Nsinteger) Buttonindex {
Photo Album 0 Photo 1
Switch (buttonindex) {
Case 0:
Read from the album
[Self readimagefromalbum];
Break
Case 1:
Photo
[Self readimagefromcamera];
Break
Default
Break
}
}
3. Realize the function of reading pictures from the album, the code is as follows
Read from the album
-(void) Readimagefromalbum {
Creating objects
Uiimagepickercontroller *imagepicker = [[Uiimagepickercontroller alloc] init];
(select type) to select a photo from the album only
Imagepicker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;
Specify the proxy, so we want to implement Uiimagepickercontrollerdelegate, Uinavigationcontrollerdeleg ATE 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];
}
4. Achieve photo function
-(void) Readimagefromcamera {
if ([Uiimagepickercontroller ISSOURCETYPEAVAILABLE:UIIMAGEPICKERCONTROLLERSOURC Etypecamera]) {
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];
} else {
Pop-up window responds to click events
Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Warning" message:@ "Camera not detected" Delegate:nil Canc Elbuttontitle:nil otherbuttontitles:@ "OK", nil];
[Alert show];
}
}
5. The picture completes the processing to submit, the Proxy method uipickercontrollerdelegate
Processing after the picture is finished
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (UIImage *) image editinginfo: (Nsdictionary *) Editinginfo {
Image is the modified photo
Add a picture to the corresponding view
[_headimageview Setimage:image];
End action
[Self dismissviewcontrolleranimated:yes completion:nil];
}
iOS settings avatar (access to System albums, local uploads)