NSInvalidArgumentException', reason: 'The Live Photo type cannot be specified without the Image media type'

huangapple go评论62阅读模式
英文:

NSInvalidArgumentException', reason: 'The Live Photo type cannot be specified without the Image media type'

问题

我正在尝试仅从我的相册中获取实况照片以进行处理任务。但是,以下代码会导致运行时崩溃:

let imagePicker = UIImagePickerController()
imagePicker.sourceType = .photoLibrary
//imagePicker.mediaTypes = [kUTTypeImage, kUTTypeLivePhoto] as [String]
imagePicker.mediaTypes = [kUTTypeLivePhoto] as [String]
imagePicker.delegate = self
present(imagePicker, animated: true, completion: nil)

错误:NSInvalidArgumentException,原因:不能指定实况照片类型而不包括图像媒体类型。

巧合的是,使用PHPhotosPickerSwiftUI(iOS 16+)可以成功获取仅实况照片:

PhotosPicker(selection: $viewModel.imageSelection,
                             matching: .livePhotos,
							 photoLibrary: .shared())

因此,想知道前面的代码有什么问题?

英文:

I'm trying to fetch only Live Photos from my gallery for processing tasks. However, the below code gives a runtime crash:

        let imagePicker = UIImagePickerController()
        imagePicker.sourceType = .photoLibrary
        //imagePicker.mediaTypes = [kUTTypeImage, kUTTypeLivePhoto] as [String]
        imagePicker.mediaTypes = [kUTTypeLivePhoto] as [String]
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)

Error: NSInvalidArgumentException', reason: 'The Live Photo type cannot be specified without the Image media type'

Coincidentally, using PHPhotosPicker with SwiftUI (iOS 16+) successfully fetches live photos only:

PhotosPicker(selection: $viewModel.imageSelection,
                             matching: .livePhotos,
							 photoLibrary: .shared())

So, curious to know what's wrong with the former code?

</details>


# 答案1
**得分**: 1

以下是翻译好的部分:

「UIImagePickerController」的[文档](https://developer.apple.com/documentation/uikit/uiimagepickercontroller)中指出:

&gt; 当用户在图像选择器中选择 Live Photo 时,如果要获取完整的动态图像和声音内容,*必须*在 mediaTypes 数组中同时包含 kUTTypeImage 和 kUTTypeLivePhoto 类型。

但更重要的是,在 iOS 14.0 中已弃用 `.photoLibrary` 源类型。[文档](https://developer.apple.com/documentation/uikit/uiimagepickercontroller/sourcetype/photolibrary)中指出:

&gt; 已弃用
请改用 PHPickerViewController。

只能使用 `UIImagePickerController` 来使用相机拍照。要选择现有媒体,请使用 `PHPickerViewController`。它具有更多有用的功能,并支持本例所需的内容。

<details>
<summary>英文:</summary>

The [documentation](https://developer.apple.com/documentation/uikit/uiimagepickercontroller) for `UIImagePickerController` states:

&gt;To obtain the full motion and sound content when the user chooses a Live Photo with the image picker, you *must* include both the kUTTypeImage and kUTTypeLivePhoto types in the mediaTypes array.

But more importantly, the `.photoLibrary` source type was deprecated in iOS 14.0. The [documentation](https://developer.apple.com/documentation/uikit/uiimagepickercontroller/sourcetype/photolibrary) states:

&gt;Deprecated  
Use PHPickerViewController instead.

Only use `UIImagePickerController` to take photos with the camera. For selecting existing media, use `PHPickerViewController`. It has many more useful features. And it supports what you want in this case.


</details>



huangapple
  • 本文由 发表于 2023年6月5日 02:01:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定