7

为了获取照片的创建日期,所以在显示 PHPickerViewController 之前使用 requestAuthorizationForAccessLevel。

    PHAccessLevel level = PHAccessLevelReadWrite;
    [PHPhotoLibrary requestAuthorizationForAccessLevel:level handler:^(PHAuthorizationStatus status) {
            if (status == PHAuthorizationStatusLimited || status == PHAuthorizationStatusAuthorized) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    PHPickerConfiguration *configuration = [[PHPickerConfiguration alloc] initWithPhotoLibrary:[PHPhotoLibrary sharedPhotoLibrary]];
                    configuration.filter = [PHPickerFilter imagesFilter];
                    configuration.selectionLimit = 1;
                    PHPickerViewController *picker = [[PHPickerViewController alloc] initWithConfiguration:configuration];
                    picker.delegate = self;
                    [self showViewController:picker sender:nil];
                });
            }
    }];

虽然状态是 .limited,但 iOS 14 仍然显示所有图像。

如何使用 PHPickerViewController 仅获取有限的照片?

4

1 回答 1

6

因此,iOS 14 中发生了一些变化,让我们一步一步看

1.如何读取PHPhotoLibrary访问权限状态

老的

let status = PHPhotoLibrary.authorizationStatus()

新的

let status = PHPhotoLibrary.authorizationStatus(for: .readWrite)

2.如何申请PHPhotoLibrary访问权限

老的

PHPhotoLibrary.requestAuthorization { status in
 //your code               
 }

新的

PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
      switch status {
          case .limited:
               print("limited access granted")
                
          default:
               print("denied, .restricted ,.authorized")
                
      }
  }

如果用户授予您有限的权限,您有责任显示如下示例代码所示的画廊

if status == .limited {
     PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
}

当您呈现LimitedLibraryPicker 时,上一个会话中选定的图像将已被标记为选中,并且屏幕顶部会显示一条消息-“选择更多照片或取消选择以删除访问权限

在此处输入图像描述

如果用户授予您有限的访问权限,您仍然使用 UIImagePickerController 或第三方库(如 BSImagePicker)呈现正常图库,即使您可以选择并导入您的应用程序,也会显示包含所有图片的图库,但在 Xcode 12 控制台中它会显示警告如下

Failed to decode image
[ImageManager] Failed to get sandbox extension for url: file///filepath/5003.JPG, error: Error Domain=com.apple.photos.error Code=41008 "Invalid asset uuid for client" UserInfo={NSLocalizedDescription=Invalid asset uuid for client}
于 2020-10-08T12:16:41.430 回答