Picker 的配置不是有效配置,Swift
Picker's configuration is not a valid configuration, Swift
我正在开发 Apple 在 WWDC2020 中提供的名为 PHPicker 的新图像选择器 API。当我 select 第二次从选择器中获取图像时出现此错误。代码第一次完美运行。第二次当我点击按钮打开选择器时,应用程序崩溃并出现以下错误。 “由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:‘选择器的配置不是有效配置。’”。
@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()
}
}
我使用的代码与 apple 在其网站上提供的代码相同。
https://developer.apple.com/documentation/photokit/phpickerviewcontroller。我刚刚将 selectionLimit 从 0.
更改为 1
你的代码肯定有问题。
我有 copy-pasted 你的解决方案,它在模拟器 (iOS 15.0) 上正常工作。
显然有记录(开玩笑,它在头文件中)preselectedAssetIdentifiers
属性 仅在 selectionLimit
大于 1 时有效,这是默认值。
这里是苹果开发者论坛的回答,供参考:https://developer.apple.com/forums/thread/705493
我正在开发 Apple 在 WWDC2020 中提供的名为 PHPicker 的新图像选择器 API。当我 select 第二次从选择器中获取图像时出现此错误。代码第一次完美运行。第二次当我点击按钮打开选择器时,应用程序崩溃并出现以下错误。 “由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:‘选择器的配置不是有效配置。’”。
@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()
}
}
我使用的代码与 apple 在其网站上提供的代码相同。 https://developer.apple.com/documentation/photokit/phpickerviewcontroller。我刚刚将 selectionLimit 从 0.
更改为 1你的代码肯定有问题。 我有 copy-pasted 你的解决方案,它在模拟器 (iOS 15.0) 上正常工作。
显然有记录(开玩笑,它在头文件中)preselectedAssetIdentifiers
属性 仅在 selectionLimit
大于 1 时有效,这是默认值。
这里是苹果开发者论坛的回答,供参考:https://developer.apple.com/forums/thread/705493