【问题标题】:How to filter the images shown on the PHPickerViewController to those selected for limited access如何将 PHPickerViewController 上显示的图像过滤为选择受限访问的图像
【发布时间】:2026-02-02 02:20:10
【问题描述】:

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

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

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

  2. 他们选择照片库

  3. 授权提醒显示选择照片...、允许访问所有照片或不允许

  4. 他们点击选择照片 = .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
            }
        }
    }
}

【问题讨论】:

    标签: swift xcode phphotolibrary ios14.5


    【解决方案1】:

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

    但您的代码首先从照片库中读取。你在说

    config = PHPickerConfiguration()
    

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

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

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

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

    【讨论】:

    • 感谢您的回复。我认为有限的访问权限意味着用户允许应用访问他们的照片库。
    • 是,但您没有访问照片库。
    • 只是为了清楚起见,以防有人想回答我的问题。我发布的不是完整的代码,只是与我的问题相关的部分。我确实需要对照片库的读取权限,以便用户可以将照片带入应用程序。所以我需要先申请访问权限。
    • 但我的回答是成立的。用户授权的性质或缺乏授权对您的选择器的行为没有影响。你混淆了两个不同的东西。
    最近更新 更多