swift5 如何在用户授予对 select 某些照片的有限访问权限后仅显示有限的照片库

swift5 how to only display limited photo library after user has granted limited access to select some photos

在 WWDC2020 中,引入了新的 PHPickerViewController 和新的 PHPhotoLibrary.authorizationStatus(受限)。但我遇到了以下问题:

  1. 当用户点击一个按钮以显示苹果的多个图像选择器并将请求授权显示为代码时:

     let requiredAccessLevel: PHAccessLevel = .readWrite
     PHPhotoLibrary.requestAuthorization(for: requiredAccessLevel) { (authorizationStatus) in
           switch authorizationStatus {
           case .authorized:
               DispatchQueue.main.async {
                   self.presentImagePicker()
                }
           case .limited:
               DispatchQueue.main.async {
                   self.presentImagePicker()
                }
           default:
               break
           }
    }
    
  2. self.presentImagePicker() 函数:

     func presentImagePicker() {
     var configuration = PHPickerConfiguration(photoLibrary: .shared())
     configuration.filter = .images
     configuration.selectionLimit = self.imageCountMax - self.images.count
     let picker = PHPickerViewController(configuration: configuration)
     picker.delegate = self
    
     let accessLevel: PHAccessLevel = .readWrite
     let authorizationStatus = PHPhotoLibrary.authorizationStatus(for: accessLevel)
    
     switch authorizationStatus {
     case .authorized:
         DispatchQueue.main.async {
    
             self.present(picker, animated: true)
         }
     case .limited:
         DispatchQueue.main.async {
             // Here I don't know how to display only limited photo library to users (after user has selected some photos through the limited access)
         }
     default:
         break
     }
    

    }

我的问题:请看代码2,case .limited: DispatchQueue.main.async { },我想我应该把有限的照片库放在这个块里,但我不知道如何只显示有限的照片图书馆给用户。

你可以使用这个PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)方法进入.limited

DispatchQueue.main.async {
         // Here I don't know how to display only limited photo library to users (after user has selected some photos through the limited access)
        PHPhotoLibrary.shared().register(self)
        PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)
     }

然后您必须使用委托方法来获取更新的图像。

func photoLibraryDidChange(_ changeInstance: PHChange) {
        let fetchOptions = PHFetchOptions()
        self.allPhotos = PHAsset.fetchAssets(with: .image, options: fetchOptions)
    }