2

如何将 PHPickerViewController 上显示的图像过滤为用户在有限访问权限下选择的图像?还是我需要使用不同的选择器?我已经为此苦苦挣扎了几天。任何帮助,将不胜感激。提前致谢。

当用户点击按钮时:1-5是自动的

  1. 警报与相机或照片库一起出现

  2. 他们选择照片库

  3. 授权警报出现选择照片...、允许访问所有照片或不允许

  4. 他们点击 Select Photos = .limited

  5. 显示 presentLimitedLibraryPicker,供用户选择要允许的照片并点击完成。

  6. 现在我希望选择器显示用户刚刚选择的图像的过滤选择。似乎这也是自动的。不是...

这仅显示用户为受限访问做出选择的相同选择器。

PHPhotoLibrary.shared().presentLimitedLibraryPicker(from: self)

.limited 案例中发生了什么?

var config = PHPickerConfiguration()
config.selectionLimit = 1
config.filter = PHPickerFilter.any(of: [.images, .livePhotos])
let picker_Photo = PHPickerViewController(configuration: config)
picker_Photo.delegate = self

let libCell = UIAction(title: "Photo Library", image: UIImage(systemName: "photo"), identifier: .none, discoverabilityTitle: .none) { (libAction) in
                
    if UIImagePickerController.isSourceTypeAvailable(.photoLibrary)
    {
        PHPhotoLibrary.requestAuthorization(for: .readWrite) { status in
            
            switch status
            {
            case .limited:
                DispatchQueue.main.async
                {
                    // PHPhotoLibrary.shared().register(self)
                    // PHPhotoLibrary.shared().presentLimitedLibraryPicker
                    self.present(picker_Photo, animated: true, completion: nil)
                }
                
            case .authorized:
                DispatchQueue.main.async
                {
                    self.present(picker_Photo, animated: true, completion: nil)
                }
                
            case .notDetermined:
                DispatchQueue.main.async
                {
                    self.present(picker_Photo, animated: true, completion: nil)
                }
                
            case .restricted:
                self.view.sendConfirmationAlert(theTitle: "Photo Library Restricted",
                                                theMessage: "Photo Library access was previously denied. Please update your Settings to allow access.", buttonTitle: "Ok")
                
            case .denied:
                let settingsAlert = UIAlertController(title: "Photo Library Access Denied",
                                                      message: "Photo Library access was previously denied. Please update your Settings to allow access.", preferredStyle: .alert)
                
                let settingsAction = UIAlertAction(title: "Go to Settings", style: .default) { ( action ) in
                    let settingsUrl = URL(string: UIApplication.openSettingsURLString)
                    UIApplication.shared.open(settingsUrl!, options: [:])
                }
                let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
                DispatchQueue.main.async
                {
                    settingsAlert.addAction(settingsAction)
                    settingsAlert.addAction(cancelAction)
                    self.present(settingsAlert, animated: true)
                }
                
            default:
                return
            }
        }
    }
}
4

1 回答 1

0

您误解了有限访问的含义。它限制了您可以在照片库中读取和写入的信息。

但是您的代码一开始并没有从照片库中读取。你是说

config = PHPickerConfiguration()

因此,这意味着您根本无法通过选择器访问照片库。您只能获取图像副本。因此,您不需要任何类型的权限来显示选择器,并且用户限制对您没有任何影响。实际上,您可以丢弃所有与授权相关的代码;它没有效果。没有它,您的选择器将完全一样地工作。

如果您需要通过选择器访问照片库,您会调用

https://developer.apple.com/documentation/photokit/phpickerconfiguration/3616114-init

在这种情况下,用户限制对您来说很重要——当您尝试访问实际的 PHAsset 时。但是您的代码从不这样做。您正在请求您不需要的权限。

于 2021-06-19T05:40:53.713 回答