我试图使用PHPickerViewController作为UIImagePickerController的替代品来选择资产,但是当设置filter和config选项时,我看不到设置返回的视频长度限制的方法。也就是说,人们通过videoMaximumDuration使用UIImagePickerController的方式。
有人发现有什么运动吗?
private func presentPicker(filter: PHPickerFilter?)-> PHPickerViewController {
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 singular selection.
configuration.selectionLimit = 1
// Set the preselected asset identifiers with the identifiers that the app tracks.
configuration.preselectedAssetIdentifiers = []
let picker = PHPickerViewController(configuration: configuration)
//picker.delegate = self
//present(picker, animated: true)
return picker
}发布于 2021-12-28 06:21:58
通过查看PHPickerFilter和PHPickerViewController的文档,我看不到任何按视频长度进行过滤的原生方式。
我能想到的最好的解决方案是遵循委托PHPickerViewControllerDelegate,并在用户选择它之后检查视频长度。
您还没有提供其余的代码,但是您应该遵守PHPickerViewControllerDelegate并实现所需的方法:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
guard let provider = results.first?.itemProvider else { return }
provider.loadItem(forTypeIdentifier: UTType.movie.identifier, options: [:]) { (videoURL, error) in
// Check the video length here
}
}
}有关如何加载视频的示例,请参阅上面的代码。从这里,您可以跟踪一些现有的答案,看看如何提取视频的持续时间。如果视频持续时间在您想要筛选的范围内,请使用FileManager将其复制到适当的位置,并按照您认为合适的方式处理它,如果它没有 (IE太长,或者太短,您不喜欢),然后丢弃它并使用警告/消息告诉用户。
下面是一个关于如何获取视频持续时间的示例。注意:您只需要替换对完成处理程序中访问的videoURL的urls的引用(请参见注释)。
Get the accurate duration of a video
另一种解决方案是使用来自第三方的更强大的照片选择器。
这有一个缺点,要求用户向他们的整个照片库提供权限(因为照片选择器不需要提示权限,因为它是提供的iOS &沙箱视图控制器)。它还在应用程序中引入了另一个依赖项。
如果这些缺点适合您的用例,那么这可能正是您所要寻找的。
下面是一个库,它提供了一些更强大的过滤器:https://github.com/Yummypets/YPImagePicker
请参阅自述的“视频”部分。它允许您指定最小和最大视频持续时间。
https://stackoverflow.com/questions/70503210
复制相似问题