Search code examples
iosswiftmetadataexifphpickerviewcontroller

iOS PHPickerViewController does not provide Exif/Meta-Data


I am trying to read out some metadata from images which I fetched with the PHPickerViewController.

var config = PHPickerConfiguration()
self.phPicker = PHPickerViewController(configuration: config)
if let picker = self.phPicker {
    self.phPicker?.delegate = self
    self.present(picker, animated: true) {
        self.phPicker = nil
    }
}

But the PHPickerResult.assetIdentifier to access those data is always nil.

extension ChatViewController: PHPickerViewControllerDelegate {
    func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
        dismiss(animated: true) {
            if results.isEmpty {
                return
            }
            assert(results.first!.assetIdentifier != nil)  // fails
        }
    }
}

Solution

  • The trick is the correct configuration of the PHPickerViewController.

    var config = PHPickerConfiguration()  // WRONG!
    let picker = PHPickerViewController(configuration: config)
    

    According to documentation you have to use the PHPhotoLibrary.

    A PHPhotoLibrary provides access to the metadata and image data for the photos, videos and related content in the user's photo library, including content from the Camera Roll, iCloud Shared, Photo Stream, imported, and synced from iTunes.

    So just use PHPhotoLibrary in the constructor and everything works fine.

    var config = PHPickerConfiguration(photoLibrary: PHPhotoLibrary.shared())  // CORRECT!
    let picker = PHPickerViewController(configuration: config)