点击图片 更换头像
点击图片 更换头像
<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择", nil];
[sheet showInView:self.view];
[sheet release];
}
#pragma mark -- imagePickerControllerDelegate
//获取图片后触发
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - handle Photo
//action sheet 响应事件
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//创建对象
UIImagePickerController *imagePicker = [[[UIImagePickerController alloc] init] autorelease];
//相片来源:1.photoAlum 2.camera 3.photoLibrary
//指定相片来源
switch (buttonIndex) {
case 0: //拍照
//判断是否支持相机
if ([UIImagePickerController isSourceTypeAvailable:(UIImagePickerControllerSourceTypeCamera)]) {
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"不支持相机拍照,请重新选择" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil, nil];
[alert show];
[alert release];
return;
}
break;
case 1:
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; //从相册选择
break;
case 2:
return; //取消
default:
break;
}
//跳转模式
imagePicker.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
//指定代理
imagePicker.delegate = self;
//弹出视图
[self presentViewController:imagePicker animated:YES completion:nil];
}