0

我一直在尝试从 swifui 应用程序的照片库中加载图像。我正在运行 Xcode 13.2.1 并构建 IOS 15.2 我的代码如下


    @Binding var image: UIImage?

    func makeUIViewController(context: Context) -> PHPickerViewController {

        var config = PHPickerConfiguration()

        config.filter = .images

        config.selectionLimit = 1

        config.preferredAssetRepresentationMode = .compatible

        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 act as your PHPickerViewControllerDelegate

    class Coordinator: NSObject, PHPickerViewControllerDelegate {
        private let parent: PhotoPicker

        init(_ parent: PhotoPicker) {

            self.parent = parent

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

            picker.dismiss(animated: true)

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

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

            print("Invoking getPhoto")

            self.getPhoto2(from: itemProvider)

            //parent.didFinishPicking(!results.isEmpty)

        }

        private func getPhoto2(from itemProvider: NSItemProvider) {

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

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

                    self.parent.image = image as? UIImage

                    print("Loaded Image \(error)")

                }

            }

        }
}
}

在控制台上,我看到以下错误

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

此外,当我打印我看到的结果时

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

    "public.jpeg",

    "public.heic"

)}, assetIdentifier: nil)]

它看起来不像 loadObject 上的错误有值。并且资产标识符为零是可疑的。

关于我可能在这里遗漏的任何想法都会很有帮助。

更新:

我尝试使用 loadFileRepresentation 代替。

itemProvider.loadFileRepresentation(forTypeIdentifier: UTType.jpeg.identifier) { (url, error) in
                guard let url = url else {
                    print("Failed to load url")
                    return
                }

这次我在控制台上收到以下错误。

  Error copying file type public.jpeg. Error: Error Domain=NSItemProviderErrorDomain Code=-1000 "Cannot load representation of type public.jpeg" UserInfo={NSLocalizedDescription=Cannot load representation of type public.jpeg, NSUnderlyingError=0x600001d26430 {Error Domain=NSCocoaErrorDomain Code=4101 "Couldn’t communicate with a helper application." UserInfo={NSUnderlyingError=0x600001dbcb70 {Error Domain=PHAssetExportRequestErrorDomain Code=0 "The operation couldn’t be completed. (PHAssetExportRequestErrorDomain error 0.)" UserInfo={NSLocalizedDescription=The operation couldn’t be completed. (PHAssetExportRequestErrorDomain error 0.), NSUnderlyingError=0x600001dbca80 {Error Domain=PAMediaConversionServiceErrorDomain Code=2 "The operation couldn’t be completed. (PAMediaConversionServiceErrorDomain error 2.)" UserInfo=0x600001398e60 (not displayed)}}}}}}
4

0 回答 0