为什么phpickerviewController说“无法加载照片”

发布于 2025-01-24 03:59:10 字数 2374 浏览 0 评论 0原文

我正在研究一个项目,我需要用户能够从相机卷中选择图像。我当时正在使用uiimagePickerController,但由于某种原因,它不再起作用,我认为这是因为他们终于对imagePicker进行了排名。我不是使用phpickerviewController,但是每次弹出时,它都会使用重新按钮“无法加载照片”,甚至单击该按钮也不会加载照片。

我希望用户能够选择一个CollectionView单元格,该单元格使PhpickerviewController弹出。然后,他们选择一张照片照片,然后完成收集视图,以使用被窃听的单元格中的所选图像进行更新。

这是我介绍phpickerviewController的代码:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    var configuration = PHPickerConfiguration(photoLibrary: .shared())
    configuration.selectionLimit = 3
    configuration.filter = .images
    let picker = PHPickerViewController(configuration: configuration)
    picker.delegate = self
    self.present(picker, animated: true, completion: nil)
}

我在viewDidload()的内部使用此方法来检查照片权限:

func photoAuthorization() {
    PHPhotoLibrary.requestAuthorization { status in
        switch status {
        case .notDetermined:
            break
        case .restricted, .denied:
            let alert = UIAlertController(title: "Photo Access restriced or denied",
                                          message: "Please allow access to your photo library",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (_) in
                alert.dismiss(animated: true)
            }))
            
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
        case .authorized:
            break
        case .limited:
            let alert = UIAlertController(title: "Photo Access Limited",
                                          message: "Please allow full access to your photo library",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (_) in
                alert.dismiss(animated: true)
            }))
            
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
        }
    }
}

如果有人可以让我知道我是否设置了ViewController错误,或者如果需要采取其他步骤才能正确工作,这将是非常感谢的。另外,如果有更好的方法来实现此功能,请告诉我。

更新: 我尝试在实际设备而不是模拟器上运行它,并且运行良好。我不知道为什么它在模拟器中不起作用,所以也许它与我的笔记本电脑设置有关,而不是代码本身有关?有人有任何想法,为什么它可以在我的实际手机上使用,而在模拟器上不起作用?

I'm working on a project where I need the user to be able to select images from their camera roll. I was using a UIImagePickerController but for some reason it's not working anymore which I assume is because they've finally depricated the imagePicker. I'm not using a PHPickerViewController but every time it pops up it says "Unable to Load Photos" with a try again button and even clicking that button doesn't load the photos.

I want the User to be able to select a collectionView cell which makes the PHPickerViewController pop up. Then they select a photo photo and then, once done the collectionView to update with the selected image in the cell that was tapped.

Here's the code where I present the PHPickerViewController:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    var configuration = PHPickerConfiguration(photoLibrary: .shared())
    configuration.selectionLimit = 3
    configuration.filter = .images
    let picker = PHPickerViewController(configuration: configuration)
    picker.delegate = self
    self.present(picker, animated: true, completion: nil)
}

And I use this method inside of viewDidLoad() to check photo permissions:

func photoAuthorization() {
    PHPhotoLibrary.requestAuthorization { status in
        switch status {
        case .notDetermined:
            break
        case .restricted, .denied:
            let alert = UIAlertController(title: "Photo Access restriced or denied",
                                          message: "Please allow access to your photo library",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (_) in
                alert.dismiss(animated: true)
            }))
            
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
        case .authorized:
            break
        case .limited:
            let alert = UIAlertController(title: "Photo Access Limited",
                                          message: "Please allow full access to your photo library",
                                          preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Okay", style: .default, handler: { (_) in
                alert.dismiss(animated: true)
            }))
            
            DispatchQueue.main.async {
                self.present(alert, animated: true)
            }
        }
    }
}

If someone could let me know if I'm setting the viewController up wrong or if there are additional steps I need to take in order for it to work correctly it would be very much appreciated. Also if there is a better way to achieve this functionality please let me know.

Update:
I tried running it on my actual device rather than the simulator and it runs just fine. I don't know why it doesn't work in the simulator, so maybe it has something to do with my laptops settings rather than the code itself? Anyone have any ideas why it would work on my actual phone but not the simulator?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文