英文:
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,原因:不能指定实况照片类型而不包括图像媒体类型。
巧合的是,使用PHPhotosPicker与SwiftUI(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)中指出:
> 当用户在图像选择器中选择 Live Photo 时,如果要获取完整的动态图像和声音内容,*必须*在 mediaTypes 数组中同时包含 kUTTypeImage 和 kUTTypeLivePhoto 类型。
但更重要的是,在 iOS 14.0 中已弃用 `.photoLibrary` 源类型。[文档](https://developer.apple.com/documentation/uikit/uiimagepickercontroller/sourcetype/photolibrary)中指出:
> 已弃用
请改用 PHPickerViewController。
只能使用 `UIImagePickerController` 来使用相机拍照。要选择现有媒体,请使用 `PHPickerViewController`。它具有更多有用的功能,并支持本例所需的内容。
<details>
<summary>英文:</summary>
The [documentation](https://developer.apple.com/documentation/uikit/uiimagepickercontroller) for `UIImagePickerController` states:
>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:
>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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论