获取手机本地的图片或者照相机照像的图片 为头像
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直接调用就好
判断设备是否具有照像机功能有的话执行前者方法没有执行后者方法
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直接调用就好
相关文章推荐
- 优化SQLServer数据库加快查询速度
- 日经春秋 20151219
- 天声人語 20151219 手書きの年賀状
- callable,compile,eval,single,exec,递归
- C++类模板的定义
- xTaskCreate()
- java线程并发控制:ReentrantLock Condition使用详解
- 308. Range Sum Query 2D - Mutable
- Linux 基金会宣布开发区块链技术的新团队
- Linux 基金会宣布开发区块链技术的新团队
- Android:Handler+Looper+MessageQueue+Thread(线程间的通信)随记
- 求js和php怎样生成随机数,0到1之间的,保留两位小数点。
- 备忘-设计模式
- RBAC权限管理
- iOS中出现循环引用时常见的三种情况
- Java RMI学习
- 关于实现自定义浏览器框架的相关细节问题
- 再谈IE的浏览器模式和文档模式[转]
- Day 02
- 数列求和