自己总结的修改头像的方法,只为方便自己查询使用!转发
步骤:1、遵守代理协议
<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate>
2、点击事件{
UIActionSheet*choosePhotoActionSheet;
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
choosePhotoActionSheet = [[UIActionSheetalloc]initWithTitle:NSLocalizedString(@"选取图片",@"")
delegate:self
cancelButtonTitle:NSLocalizedString(@"取消",@"")
destructiveButtonTitle:nil
otherButtonTitles:NSLocalizedString(@"相机",@""),NSLocalizedString(@"相册",@""),nil];
}else{
choosePhotoActionSheet = [[UIActionSheetalloc]initWithTitle:NSLocalizedString(@"选取图片",@"")
delegate:self
cancelButtonTitle:NSLocalizedString(@"取消",@"")
destructiveButtonTitle
:nil
otherButtonTitles:NSLocalizedString(@"相册",@""),nil];
}
[choosePhotoActionSheetshowInView:self.view];
}
3、实现代理方法
#PRagma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSUIntegersourceType =0;
if([UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch(buttonIndex) {
case0:
sourceType =UIImagePickerControllerSourceTypeCamera;
break;
case1:
sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
break;
case2:
return;
}
}else{
if(buttonIndex ==1) {
return;
}else{
sourceType =UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
UIImagePickerController*imagePickerController = [[UIImagePickerControlleralloc]init];
imagePickerController.delegate=self;
imagePickerController.allowsEditing=YES;
imagePickerController.sourceType= sourceType;
[selfpresentViewController:imagePickerControlleranimated:YEScompletion:^{
}];
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
self.tmpHeaderImg= [infoobjectForKey:UIImagePickerControllerEditedImage];
[self.imageViewUserHeadsetImage:self.tmpHeaderImg];
[selfuploadImage];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[selfdismissViewControllerAnimated:YEScompletion:^{
}];
}