您的位置:首页 > 其它

获取手机本地的图片或者照相机照像的图片 为头像

2015-12-19 11:01 239 查看
第一步点击获取头像按钮 下面为按钮执行方法
判断设备是否具有照像机功能有的话执行前者方法没有执行后者方法
UIActionSheet *actionSheet;
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
actionSheet = [[UIActionSheet alloc]initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"从相册选择", nil,nil];
}else{
actionSheet = [[UIActionSheet alloc] initWithTitle:@"选择图像" delegate:self cancelButtonTitle:@"取消"destructiveButtonTitle:nil otherButtonTitles:@"从相册选择", nil];
}

actionSheet.tag = 1000;
[actionSheet showInView:self.view];
第二步 执行调用相机或者相册方法 当然了还要引用代理UINavigationControllerDelegate, UIImagePickerControllerDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
if (actionSheet.tag == 1000) {
NSUInteger sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
// 判断是否支持相机
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case 0:

sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
//来源:相册

break;
case 2:
return;
}
}
else {
if (buttonIndex == 2) {
return;
} else {
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
}
// 跳转到相机或相册页面
UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
imagePickerController.delegate = self;
imagePickerController.allowsEditing = YES;
imagePickerController.sourceType = sourceType;

[self presentViewController:imagePickerController animated:YES completion:^{

}];
}
}
第三步就是确定头头像了
#pragma mark - 头像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:nil];

UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
NSLog(@"%@",image);
_faceImage = image;

[_tableView reloadData];

}
第四步就是你要把选中的图片显示在上面地方就显示在上面地方 这图片就是 _faceImage直接调用就好
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: