<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
检测前置摄像头是否可用
- (BOOL)isFrontCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]; }
检测后置摄像头是否可用
- (BOOL)isRearCameraAvailable{ return [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]; }
调用相机
///调用相机 - (void)persentImagePicker{ if (!_imagePickerG) { ///初始化相机 _imagePickerG = [[UIImagePickerController alloc]init]; ///代理 _imagePickerG.delegate = self; } // 前面的摄像头是否可用 if ([self isFrontCameraAvailable]) { _imagePickerG.sourceType = UIImagePickerControllerSourceTypeCamera; } // 后面的摄像头是否可用 else if ([self isFirstResponder]){ _imagePickerG.sourceType = UIImagePickerControllerSourceTypeCamera; } else{ [SVProgressHUD showErrorWithStatus:@"没有相机可用~"]; return; } ///允许拍照后裁剪 _imagePickerG.allowsEditing = YES; [self.navigationController presentViewController:_imagePickerG animated:YES completion:nil]; }
///调用本地相册 - (void)persentImagePicker{ if (!_imagePickerG) { ///初始化相机 _imagePickerG = [[UIImagePickerController alloc]init]; ///代理 _imagePickerG.delegate = self; } ///相册 _imagePickerG.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; _imagePickerG.allowsEditing = YES; [self.navigationController presentViewController:_imagePickerG animated:YES completion:nil]; }
///取消选择图片(拍照) - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [picker dismissViewControllerAnimated:YES completion:nil]; }
///选择图片完成(从相册或者拍照完成) - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];///原图 //获取修剪后的图片 UIImage *imageUp = [info objectForKey:UIImagePickerControllerEditedImage]; }
///保存图片到本地相册 -(void)imageTopicSave:(UIImage *)image{ UIImageWriteToSavedPhotosAlbum(image, self, @selector(image: didFinishSavingWithError: contextInfo:), nil); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{ if (error == nil) { } else{ ///图片未能保存到本地 } }