iOS相机相册的正确打开方式- UIImagePickerController

通过指定sourceType来实现打开相册还是相机

UIImagePickerControllerSourceTypePhotoLibrary, // 相册库,范围比PhotosAlbum要大
UIImagePickerControllerSourceTypeCamera, // 相机
UIImagePickerControllerSourceTypeSavedPhotosAlbum // 相册

打开之前一定判断设备是否可用[UIImagePickerController isSourceTypeAvailable:type]

/// 打开相机
- (void)openCamera
{
    [self openImagePickerControllerWithType:UIImagePickerControllerSourceTypeCamera];
}
/// 打开相册
- (void)openAlbum
{
    [self openImagePickerControllerWithType:UIImagePickerControllerSourceTypePhotoLibrary];
}
/// 打开ImagePickerController的方法
- (void)openImagePickerControllerWithType:(UIImagePickerControllerSourceType)type
{
    // 设备不可用  直接返回
    if (![UIImagePickerController isSourceTypeAvailable:type]) return;

    UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
    imgPicker.sourceType = type;
    imgPicker.delegate = self;
    [self presentViewController:imgPicker animated:YES completion:nil];
}

代理方法中,获取选中图片注意:一定记得dismiss

#pragma mark - UINavigationControllerDelegate, UIImagePickerControllerDelegate
// 选择照片之后
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//    ChaosLog(@"%@",info);
    // 获取用户选择的图片
    UIImage *image = info[UIImagePickerControllerOriginalImage];

    [self.photosView addPhoto:image];

    // 退出imagePickerController
    [self dismissViewControllerAnimated:YES completion:nil];
    [self.textView becomeFirstResponder];
}

系统自带的只能单选,实现多选要用大如图的库

iOS开发小技巧–相机相册的正确打开方式-冯金伟博客园