iOS PHPickerViewController基础用法 OC

PHPickerViewController 作为iOS14之后的图片视频选择器,在多选和资源处理方面要优胜与UIImagePickerController

导入:

#import <PhotosUI/PhotosUI.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>

代理:

<PHPickerViewControllerDelegate>

创建:

PHPickerConfiguration *config = [[PHPickerConfiguration alloc] init];//配置
config.selectionLimit = 0;//选择数量,0为多选,默认为1
config.filter = [PHPickerFilter imagesFilter]; //选择类型,默认为所有
config.preferredAssetRepresentationMode = PHPickerConfigurationAssetRepresentationModeAutomatic;//弹出类型
PHPickerViewController *pickerVC = [[PHPickerViewController alloc] initWithConfiguration:config];
pickerVC.delegate = self;
[self presentViewController:pickerVC animated:YES completion:nil];

代理回调,资源处理:

#pragma mark - PHPickerViewControllerDelegate
-(void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results{
    for (int i = 0; i < results.count; i++) {
        PHPickerResult *result = results[i];
        [result.itemProvider loadFileRepresentationForTypeIdentifier:UTTypeMovie.identifier completionHandler:^(NSURL * _Nullable url, NSError * _Nullable error) {
            if(!error){
               //处理视频资源
            }
        }];
        [result.itemProvider loadFileRepresentationForTypeIdentifier:UTTypeImage.identifier completionHandler:^(NSURL * _Nullable url, NSError * _Nullable error) {
            if(!error){
               //处理图片资源
            }
        }];
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [picker dismissViewControllerAnimated:YES completion:nil];
    });
}

loadFileRepresentationForTypeIdentifier处理后返回的url为临时路径,dismiss后会失效,如果要对资源做沙盒操作,需要在回调内进行移动或复制

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容