【问题标题】:swift5 how to only display limited photo library after user has granted limited access to select some photosswift5如何在用户授予有限访问权限以选择某些照片后仅显示有限的照片库
【发布时间】:2020-11-07 05:56:08
【问题描述】:

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

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

     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 { },我想我应该把有限的照片库放在这个块中,但我不知道如何只显示有限的照片库用户。

【问题讨论】:

    标签: swift photokit


    【解决方案1】:

    您可以将这个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)
        }
    
    

    【讨论】:

    • 我遇到了同样的问题。您能否解释一下您是如何声明 allPhotos 的?
    • @Quailcreek : var allPhotos: PHFetchResult!
    猜你喜欢
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    • 2019-04-17
    • 2021-01-23
    • 2014-08-11
    • 2019-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多