PHPickerViewController 作为iOS14之后的图片视频选择器,在多选和资源处理方面要优胜与UIImagePickerController
导入:
#import <Photos/Photos.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
后会失效,如果要对资源做沙盒操作,需要在回调内进行移动或复制