【问题标题】:How can [UIImagePickerController.InfoKey.phAsset] as? PHAsset be nil[UIImagePickerController.InfoKey.phAsset] 怎么能这样? PHAsset 为零
【发布时间】:2022-01-20 01:19:26
【问题描述】:

我向用户发送了我的应用的 TestFlight 版本。她录制了一段使用 iPhone 制作的视频。当 imagePicker 出现并且她选择了一个视频时,它返回为 nil。我收到一条编号为 120 的错误消息,该消息显示在一条警报中,告诉我错误发生的位置。

它只能是资产[UIImagePickerController.InfoKey.phAsset] as? PHAssetnil。我不明白这怎么可能,因为这是她用手机制作的视频。奇怪的是,当她选择照片时一切正常,而当我使用 iOS 14 和 iOS 13 选择视频时一切正常。

她使用的是 iOS 15.1,我想知道是这个问题吗?她是一名 iOS 开发人员,她说 iOS 15 一直在引起问题。

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset
    // ...

    if let style = asset?.playbackStyle {

        // ...
        // if it entered here then the below alert would have never appeared

    } else {

        let errorMessage = "Error: 120"
        let alert = UIAlertController(title: "Unknown Error", message: errorMessage, preferredStyle: .alert)
        // ...
    }

    imagePicker?.dismiss(animated: true, completion: nil)
}

仅供参考,之后我刚刚在 iOS 15.1 上测试了一些视频,效果很好。

我尝试了 PHPickerController,但它有很多问题,所以我现在宁愿坚持使用 ImagePicker。

【问题讨论】:

    标签: ios swift uiimagepickercontroller phasset


    【解决方案1】:

    我不确定资产如何为零,但根据answer by @Sh_Kahn,我需要处理所有可能性:

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    
        if let asset = info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {
    
            let style = asset.playbackStyle
            // ...
    
        } else if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
    
            // ...
    
        } else if let imageUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL {
    
            // ...
    
        } else {
    
            let errorMessage = "Error: 120"
            let alert = UIAlertController(title: "Unknown Error", message: errorMessage, preferredStyle: .alert)
            // ...
        }
    
        imagePicker?.dismiss(animated: true, completion: nil)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-03
      相关资源
      最近更新 更多