Sunday, November 8, 2026
HomeiOS Developmentios - PHPickerViewController fails to load picture

ios – PHPickerViewController fails to load picture

[ad_1]

I’ve been making an attempt to load a picture from the photograph library on a swifui app. I’m operating Xcode 13.2.1 and constructing IOS 15.2
My code is as beneath


    @Binding var picture: UIImage?

    func makeUIViewController(context: Context) -> PHPickerViewController {

        var config = PHPickerConfiguration()

        config.filter = .pictures

        config.selectionLimit = 1

        config.preferredAssetRepresentationMode = .suitable

        let controller = PHPickerViewController(configuration: config)

        controller.delegate = context.coordinator

        return controller

    }

    func updateUIViewController(_ uiViewController: PHPickerViewController, context: Context) { }

    func makeCoordinator() -> Coordinator {

        Coordinator(self)

    }
    // Use a Coordinator to behave as your PHPickerViewControllerDelegate

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        personal let father or mother: PhotoPicker

        init(_ father or mother: PhotoPicker) {

            self.father or mother = father or mother

        }
        func picker(_ picker: PHPickerViewController, didFinishPicking outcomes: [PHPickerResult]) {

            picker.dismiss(animated: true)

            print(outcomes)
            guard !outcomes.isEmpty else {
                return
            }

            guard let itemProvider = outcomes.first?.itemProvider else { return }

            print("Invoking getPhoto")

            self.getPhoto2(from: itemProvider)

            //father or mother.didFinishPicking(!outcomes.isEmpty)

        }

        personal func getPhoto2(from itemProvider: NSItemProvider) {

            print("getPhoto")
         
            if itemProvider.canLoadObject(ofClass: UIImage.self) {

                itemProvider.loadObject(ofClass: UIImage.self) { picture, error in

                    self.father or mother.picture = picture as? UIImage

                    print("Loaded Picture (error)")

                }

            }

        }
}
}

On the console I see the next error

022-01-27 00:40:01.485085-0500 Vescense[3174:884964] [Picker] Exhibiting picker unavailable UI (purpose: nonetheless loading) with error: (null)

Additional after I print the end result I see

[PhotosUI.PHPickerResult(itemProvider: <PUPhotosFileProviderItemProvider: 0x2818fc980> {types = (

    "public.jpeg",

    "public.heic"

)}, assetIdentifier: nil)]

It would not seem just like the error on loadObject has a worth. And it is suspicious that assetIdentifier is nil.

Any ideas on what I is perhaps lacking right here could be most useful.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments