首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择器的配置不是有效的配置,Swift

选择器的配置不是有效的配置,Swift
EN

Stack Overflow用户
提问于 2022-04-06 10:54:55
回答 2查看 234关注 0票数 1

我正在开发由苹果公司在WWDC2020中提供的名为PHPicker的新图像选择API。当我第二次从选择器中选择图像时,我会得到这个错误。当代码第一次完美地工作时。第二次单击按钮打开选择器时,应用程序崩溃时会出现以下错误。“由于异常'NSInternalInconsistencyException‘终止应用程序,原因:’选择器的配置不是一个有效的配置.‘”。

代码语言:javascript
复制
 @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()
        }
    }

我使用的代码与苹果在他们的网站上提供的代码相同。https://developer.apple.com/documentation/photokit/phpickerviewcontroller。我刚刚把selectionLimit从0改为1。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-05-10 14:50:49

显然,只有当preselectedAssetIdentifiers大于1 (这是默认的)时,preselectedAssetIdentifiers属性才是有效的(开玩笑的,只是在头文件中)。

以下是Apple论坛的回答,供参考:https://developer.apple.com/forums/thread/705493

票数 0
EN

Stack Overflow用户

发布于 2022-04-06 11:18:28

您的代码一定有问题。我已经复制粘贴了您的解决方案,它在模拟器(iOS 15.0)上正确工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71765492

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档