PHPickerViewController 作為iOS14之后的圖片視頻選擇器,在多選和資源處理方面要優(yōu)勝與UIImagePickerController
導(dǎo)入:
#import <Photos/Photos.h>
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>
代理:
<PHPickerViewControllerDelegate>
創(chuàng)建:
PHPickerConfiguration *config = [[PHPickerConfiguration alloc] init];//配置
config.selectionLimit = 0;//選擇數(shù)量烤低,0為多選擂送,默認(rèn)為1
config.filter = [PHPickerFilter imagesFilter]; //選擇類型侈贷,默認(rèn)為所有
config.preferredAssetRepresentationMode = PHPickerConfigurationAssetRepresentationModeAutomatic;//彈出類型
PHPickerViewController *pickerVC = [[PHPickerViewController alloc] initWithConfiguration:config];
pickerVC.delegate = self;
[self presentViewController:pickerVC animated:YES completion:nil];
代理回調(diào),資源處理:
#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
后會失效撞牢,如果要對資源做沙盒操作绣硝,需要在回調(diào)內(nèi)進(jìn)行移動或復(fù)制