iOS PHPickerViewController基礎(chǔ)用法 OC

PHPickerViewController 作為iOS14之后的圖片視頻選擇器,在多選和資源處理方面要優(yōu)勝與UIImagePickerController

導(dǎo)入:

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

代理:

<PHPickerViewControllerDelegate>

創(chuàng)建:

PHPickerConfiguration *config = [[PHPickerConfiguration alloc] init];//配置
config.selectionLimit = 0;//選擇數(shù)量,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];

代理回調(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)進行移動或復(fù)制

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容