iOS拍照之系统拍照

Posted 善斋书屋

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS拍照之系统拍照相关的知识,希望对你有一定的参考价值。

拍照在App中使用频次高,入门级别直接调用系统拍照


  • 思路:

    系统拍照使用UIImagePickerController

    1.设置下plist,否则没权限,报错

    2.判断摄像头,获取权限,否则弹出界面黑着

    3.设置代理,实现代理

    4.代理方法中获取拍照的图片/视频

  • 上菜

    • plist中添加Privacy - Camera Usage Description,文案根据App需要来
    • 权限
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
    
    }
    • 设置UIImagePickerController
    UIImagePickerController *vc = [[UIImagePickerController alloc] init];
    // 设置该属性的时候,出现拍照与拍视频选项
    vc.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
    vc.sourceType = UIImagePickerControllerSourceTypeCamera;
    vc.delegate = self;
    vc.allowsEditing = NO;
    // 设置摄像头[后置摄像头]
    vc.cameraDevice = UIImagePickerControllerCameraDeviceRear;
    // 设置闪光模式
    vc.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
    // 设置摄像头模式
    vc.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo; // 当该模式设置为video的时候,meidaTypes设置出现拍照与拍视频选项时,先展示video视频拍摄选项
    // 录制的最大时间
    vc.videoMaximumDuration = 10;
    // 视频质量
    vc.videoQuality = UIImagePickerControllerQualityTypeHigh;
    vc.modalPresentationStyle = UIModalPresentationFullScreen;
    [[[[UIApplication sharedApplication] keyWindow] rootViewController] presentViewController:vc
                                                                                     animated:YES
                                                                                   completion:nil];
    • 代理实现
    #pragma mark - UINavigationControllerDelegate, UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker
    didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info {
        // 获取选中资源类型
        NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
        if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) { // 拍照结果
            UIImage *image = nil;
            if (picker.allowsEditing) {
                image = [info objectForKey:UIImagePickerControllerEditedImage];
            } else {
                image = [info objectForKey:UIImagePickerControllerOriginalImage];
            }
            [picker dismissViewControllerAnimated:YES completion:nil];
        } else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]) { // 视频结果
            NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
            // 视频存放到系统相册
            [phphotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized) {
                    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
                               [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:url];
                           } completionHandler:^(BOOL success, NSError * error) {
                               if (success) {
                                   NSLog(@"保存视频到相册");
                               } else {
                                   NSLog(@"保存视频到相册失败:%@", error);
                               }
                           }];
                } else {
                    NSLog(@"无访问相册权限");
                }
                dispatch_async(dispatch_get_main_queue(), ^{
                    [picker dismissViewControllerAnimated:YES completion:nil];
                });
            }];
        }
    }
    
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
  • 结语:

    UIImagePickerController使用方便,可拍照、视频。适用于拍照界面无定制、简单取图这类需求

以上是关于iOS拍照之系统拍照的主要内容,如果未能解决你的问题,请参考以下文章

iOS拍照定制之AVCaptureVideoDataOutput

从片段中的相机拍照

iOS之录像 拍照

iOS-蓝牙拍照

iOS 调用系统拍照遇到的坑

iOS----------调用系统照相机和相册