如何处理来自 PHPickerViewController 的视频和照片结果?
我需要用户能够从照片库中选择多张照片和视频。 (使用 PHPickerViewController)我已经知道如何用这个获取图像:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
var contentsData: [Data] = []
for result in results {
if result.itemProvider.canLoadObject(ofClass: UIImage.self) {
result.itemProvider.loadObject(ofClass: UIImage.self) { selectedData, error in
if let error = error {
print("PICKER ERROR: \(error)")
} else {
if let selectedImage = selectedData as? UIImage {
if let selectedImageData = selectedImage.jpegData(compressionQuality: 0.5) {
contentsData.append(selectedImageData)
if contentsData.count == results.count {
self.parent.completion(contentsData)
}
}
}
}
}
}
}
self.parent.presentationMode.wrappedValue.dismiss()
}
我假设我需要做的是检查 PHPickerResult 的每个结果是什么数据类型,然后相应地加载每个对象。问题是视频文件可能是 .mov、.mp4 等。如何将 PHPickerResult 识别为视频并进行相应处理?
I need the ability for a user to select multiple photos and videos from there photo library. (Using PHPickerViewController) I already know how to get images with this:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
var contentsData: [Data] = []
for result in results {
if result.itemProvider.canLoadObject(ofClass: UIImage.self) {
result.itemProvider.loadObject(ofClass: UIImage.self) { selectedData, error in
if let error = error {
print("PICKER ERROR: \(error)")
} else {
if let selectedImage = selectedData as? UIImage {
if let selectedImageData = selectedImage.jpegData(compressionQuality: 0.5) {
contentsData.append(selectedImageData)
if contentsData.count == results.count {
self.parent.completion(contentsData)
}
}
}
}
}
}
}
self.parent.presentationMode.wrappedValue.dismiss()
}
I would assume that what I would need to do is check what data type each result from PHPickerResult is and then load each object accordingly. The problem is that a video file could be .mov, .mp4, etc. How can I identify a PHPickerResult as a video and handle it accordingly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需询问项目提供者是否有
UTType.movie.identifier
类型的项目即可。如果是这样,请继续通过调用loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier)
加载它。实际代码:
Just ask whether the item provider has an item of type
UTType.movie.identifier
. If so, go ahead and load it by callingloadFileRepresentation(forTypeIdentifier: UTType.movie.identifier)
.Actual code: