引入头文件,添加代理
@interface HandleMsgPictureView () <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, strong) UIImagePickerController *picker;
@end
调用相机
- (void)takePhoto {
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
if (authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied) {
// 无相机权限
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法使用相机" message:@"请在iPhone的""设置-隐私-相机""中允许访问相机" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString] options:@{} completionHandler:nil];
}]];
[self.currentViewController presentViewController:alertController animated:YES completion:nil];
} else if (authStatus == AVAuthorizationStatusNotDetermined) {
// fix issue 466, 防止用户首次拍照拒绝授权时相机页黑屏
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self takePhoto];
});
}
}];
// 拍照之前还需要检查相册权限
} else if ([PHPhotoLibrary authorizationStatus] == 2) { // 已被拒绝,没有相册权限
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法访问相册" message:@"请在iPhone的""设置-隐私-相册""中允许访问相册" preferredStyle:UIAlertControllerStyleAlert];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
[alertController addAction:[UIAlertAction actionWithTitle:@"设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self openURLString:UIApplicationOpenSettingsURLString];
}]];
[self.currentViewController presentViewController:alertController animated:YES completion:nil];
} else if ([PHPhotoLibrary authorizationStatus] == 0) { // 未请求过相册权限
[[TZImageManager manager] requestAuthorizationWithCompletion:^{
[self takePhoto];
}];
} else {
[self.currentViewController presentViewController:self.picker animated:YES completion:nil];
}
}
#pragma mark - delegate
// 完成
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
if ([info[UIImagePickerControllerMediaType] isEqualToString:(NSString *)kUTTypeImage]) { // 拍照
// 获得原图
UIImage *Originalimage = info[UIImagePickerControllerOriginalImage];
[self.photos addObject:Originalimage];
//自己定义的数据更新
[self loadData];
} else{
NSLog(@"%@",info[UIImagePickerControllerMediaType]);
}
[self.currentViewController dismissViewControllerAnimated:YES completion:nil];
}
// 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self.currentViewController dismissViewControllerAnimated:YES completion:nil];
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
NSLog(@"error");
}
#pragma mark - lazy load
- (UIImagePickerController *)picker {
if (!_picker) {
_picker = [[UIImagePickerController alloc] init];
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;
_picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
_picker.modalPresentationStyle = UIModalPresentationFullScreen;
_picker.mediaTypes = @[(NSString *) kUTTypeImage];
_picker.delegate = self;
}
return _picker;
}