本帖最后由 竹林风 于 2018-12-11 15:28 编辑
文章导航
首先要打开项目工程里面的相机和相册权限
找到项目中的info.plist 添加如下两个权限设置
1.调用系统相机
[Objective-C] 纯文本查看 复制代码 - (void)openCamera{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
//实现协议<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
picker.delegate = self;
picker.allowsEditing = YES; //可编辑
//判断是否可以打开照相机
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
//摄像头
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:nil];
}else{
NSLog(@"没有摄像头");
}
}
2.打开相册
[Objective-C] 纯文本查看 复制代码 /**
* 打开相册
*/
-(void)openPhotoLibrary
{
// Supported orientations has no common orientation with the application, and [PUUIAlbumListViewController shouldAutorotate] is returning YES
// 进入相册
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])
{
UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:^{
NSLog(@"打开相册");
}];
}else{
NSLog(@"不能打开相册");
}
}
实现代理方法
[Objective-C] 纯文本查看 复制代码 #pragma mark - UIImagePickerControllerDelegate
// 拍照完成回调
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"finish..");
NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
// __weak typeof(self)weakSelf = self;
[picker dismissViewControllerAnimated:YES completion:^{
UIImage* image = [info objectForKey:UIImagePickerControllerEditedImage];
self->_imgPicker.image = image;
}];
}
}
//进入拍摄页面点击取消按钮
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
运行结果:
附件:
Test-CameraPhoto.zip
(158.26 KB, 下载次数: 0)
|