关于ios:iOS拍照之系统拍照

51次阅读

共计 2521 个字符,预计需要花费 7 分钟才能阅读完成。

拍照在 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 使用方便,可拍照、视频。实用于拍照界面无定制、简略取图这类需要

正文完
 0