I am working on new image picker API provided by Apple in WWDC2020 named as PHPicker. I am getting this error when I select the image form the picker second time. First time the code works perfectly. Second time when I click on the button to open the picker, app crashes with the following error. "Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘Picker’s configuration is not a valid configuration.’".
@IBAction func addVideo(_ sender: UIButton) {
presentPicker(filter: .videos)
}
private func presentPicker(filter: PHPickerFilter?) {
var configuration = PHPickerConfiguration(photoLibrary: .shared())
// Set the filter type according to the user’s selection.
configuration.filter = filter
// Set the mode to avoid transcoding, if possible, if your app supports arbitrary image/video encodings.
configuration.preferredAssetRepresentationMode = .current
// Set the selection behavior to respect the user’s selection order.
configuration.selection = .ordered
// Set the selection limit to enable multiselection.
configuration.selectionLimit = 1
// Set the preselected asset identifiers with the identifiers that the app tracks.
configuration.preselectedAssetIdentifiers = selectedAssetIdentifiers
let picker = PHPickerViewController(configuration: configuration)
picker.delegate = self
present(picker, animated: true)
}
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
dismiss(animated: true)
let existingSelection = self.selection
var newSelection = [String: PHPickerResult]()
for result in results {
let identifier = result.assetIdentifier!
newSelection[identifier] = existingSelection[identifier] ?? result
}
// Track the selection in case the user deselects it later.
selection = newSelection
selectedAssetIdentifiers = results.map(.assetIdentifier!)
selectedAssetIdentifierIterator = selectedAssetIdentifiers.makeIterator()
if selection.isEmpty {
displayEmptyImage()
} else {
displayImage()
}
}
I am using the same code that is provide by apple on their website.
https://developer.apple.com/documentation/photokit/phpickerviewcontroller. I just have changed the selectionLimit to 1 from 0.
2
Answers
There must be a problem with your code.
I’ve copy-pasted your solution and it works correctly on Simulator (iOS 15.0).
Apparently it is documented (just kidding, it’s in a header file) that the
preselectedAssetIdentifiers
property is only valid whenselectionLimit
is greater than 1, which is the default.Here’s an answer from the Apple developer forum for reference: https://developer.apple.com/forums/thread/705493