美文网首页特别重视技术相关
iOS14适配时遇到的PHPickerViewControlle

iOS14适配时遇到的PHPickerViewControlle

作者: 楠楠小胖纸 | 来源:发表于2020-09-21 14:21 被阅读0次

    网上关于适配iOS 14 时提到的使用PHPickerViewController替换UIImagePickerController,有一个细节没有留意,导致这里会产生困惑。

    如果项目中只是简单的使用到了系统相册来设置头像,可以直接使用下面的方式将UIImagePickerController替换为官方推荐的PHPickerViewController请注意,这里不需要请求相册的使用权限

    #import <PhotosUI/PhotosUI.h>
    
    @interface ViewController () <PHPickerViewControllerDelegate>
      
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
      
    @end
      
    @implementation ViewController
      
     - (void)viewDidLoad {
          [super viewDidLoad];
          // Do any additional setup after loading the view.
     }
      
     - (IBAction)button:(id)sender {
          // 以下 API 仅为 iOS14 only
          PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] init];
          configuration.filter = [PHPickerFilter imagesFilter]; // 可配置查询用户相册中文件的类型,支持三种
          configuration.selectionLimit = 1; // 默认为1,为0时表示可多选。
      
          PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
          picker.delegate = self;
          picker.view.backgroundColor = [UIColor whiteColor];//注意需要进行暗黑模式适配
          // picker vc,在选完图片后需要在回调中手动 dismiss
          [self presentViewController:picker animated:YES completion:^{
      
          }];
      }
      
    #pragma mark - Delegate
      
     - (void)picker:(PHPickerViewController *)picker didFinishPicking:(NSArray<PHPickerResult *> *)results {
          [picker dismissViewControllerAnimated:YES completion:nil];
          if (!results || !results.count) {
              return;
          }
          NSItemProvider *itemProvider = results.firstObject.itemProvider;
          if ([itemProvider canLoadObjectOfClass:UIImage.class]) {
              __weak typeof(self) weakSelf = self;
              [itemProvider loadObjectOfClass:UIImage.class completionHandler:^(__kindof id<NSItemProviderReading>  _Nullable object, NSError * _Nullable error) {
                  if ([object isKindOfClass:UIImage.class]) {
                      __strong typeof(self) strongSelf = weakSelf;
                      dispatch_async(dispatch_get_main_queue(), ^{
                          strongSelf.imageView.image = (UIImage *)object;
                      });
                  }
              }]; 
          }
       }
    

    官方对于PHPicker的说明如下:


    注意上面截图中提到的内容:

    • PHPicker will show the entire library
    • All photos and videos can be selected
    • Limited Photes Library won't be extended
    • Allow the user to pick images or video without requiring Photos Library access

    所以,官方给的建议如下:

    Also please don't prompt for photo library access before showing the picker and don't require the user to grant you access before showing the picker. There's no need to do any of this. And it doesn't help with the users trust into your app.If your app leverages PhotoKit to access the photos library please reconsider if it really needs to have access to the library or if you can use PHPicker instead.We really hope you like the new picker and API. We're looking forward to you adopting it in your apps. Thank you.

    总结:使用PHPickerViewController时,直接使用即可。

    参考视频:

    https://developer.apple.com/videos/play/wwdc2020/10652/?time=841
    https://developer.apple.com/videos/play/wwdc2020/10641/

    相关文章

      网友评论

        本文标题:iOS14适配时遇到的PHPickerViewControlle

        本文链接:https://www.haomeiwen.com/subject/qtevyktx.html