无法使用“UIImagePickerController.InfoKey”类型的参数为“[String : Any]”类型的值下标

Posted

技术标签:

【中文标题】无法使用“UIImagePickerController.InfoKey”类型的参数为“[String : Any]”类型的值下标【英文标题】:Cannot subscript a value of type '[String : Any]' with an argument of type 'UIImagePickerController.InfoKey' 【发布时间】:2019-10-23 22:35:55 【问题描述】:

我正在尝试如何更新我的代码以支持 UIImagePickerController() 在 Swift 5 中的新工作方式

我能够在此处的代码的另一部分中修复相同的错误:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) 
        guard let image = info[.originalImage] as? UIImage else  return 
        let sellLandingVC = navigationController.topViewController as? SellLandingViewController
        sellLandingVC?.viewModel.insertSelectedImage(image)
        navigationController.dismiss(animated: true, completion: nil)
    

这是我的代码,调用 imagePicker.rx.didFinishPickingMediaWithInfo.map

时出现错误
private func setupRx() 
        let imagePicker = UIImagePickerController()

        Observable.combineLatest(checkIfPermissionIsGranted(), captureButton.rx.tap)  (isGranted, _ ) in return isGranted 
            .observeOn(MainScheduler.instance)
            .subscribe(onNext:  [weak self] isGranted in
                self?.captureButton.pulsate()
                if !isGranted 
                    self?.displayAlert(with: self?.viewModel.accessDeniedTitle, message: self?.viewModel.accessDeniedMessage, actions: [UIAlertAction.cancel(), UIAlertAction.settings()])
                 else 
                    imagePicker.sourceType = .photoLibrary
                    imagePicker.allowsEditing = false
                    self?.present(imagePicker, animated: true)
                
            )
        .disposed(by: disposeBag)

        imagePicker.rx.didFinishPickingMediaWithInfo
            .map 
               $0[UIImagePickerController.InfoKey.originalImage] as! UIImage
//This is where I'm getting the error
            
            .map 
                ProfilePictureCommand.setImage($0)
            
            .bind(to: viewModel.concurrentCommandSubject)
            .disposed(by: disposeBag)

        viewModel.stateObservable
            .map 
                $0.userImage ?? #imageLiteral(resourceName: "email-signup-avatar")
        
            .bind(to: profileImageView.rx.image)
            .disposed(by: disposeBag)

        imagePicker.rx.didFinishPickingMediaWithInfo
            .subscribe(onNext:  _ in
                imagePicker.dismiss(animated: true)
            )
            .disposed(by: disposeBag)

        viewModel.stateObservable
            .map 
                return $0.isContinueEnabled
            
            .bind(to: continueButton.rx.isEnabled)
            .disposed(by: disposeBag)

        viewModel.stateObservable
            .map 
                return $0.isContinueEnabled
            
            .map 
                $0 ? UIColor.colorHex303030(alpha: 1.0) : UIColor.colorHexC5C5C5(alpha: 1.0)
            
            .bind(to: continueButton.rx.scBackgroundColor)
            .disposed(by: disposeBag)

        continueButton.rx.tap
            .map  ProfilePictureCommand.next 
            .bind(to: viewModel.concurrentThrottleCommandSubject)
            .disposed(by: disposeBag)
    

【问题讨论】:

【参考方案1】:

您的 rx 版本为您提供 [String:Any] 而不是 [UIImagePickerController.InfoKey: Any]UIImagePickerController.InfoKey 现在是一个带有.originalImage 案例的枚举。如果你想用字符串下标老式方式,只需询问原始值:UIImagePickerController.InfoKey.originalImage.rawValue

 $0[UIImagePickerController.InfoKey.originalImage.rawValue] as! UIImage

【讨论】:

以上是关于无法使用“UIImagePickerController.InfoKey”类型的参数为“[String : Any]”类型的值下标的主要内容,如果未能解决你的问题,请参考以下文章

在 UIPopoverController 中使用 UIImagePickerController 后播放视频消失

从照片中获取详细信息

在 iPad 上以纵向模式启动图像选择器

如何保存拍摄的照片

将 EXIF 数据保存为 JPEG - Swift

将导入的照片保存在 xcode 项目中