我需要一个用户选择多张照片和视频从那里的照片库的能力。(使用PHPicker)我已经知道如何使用以下方法获取图像:
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识别为视频并相应地处理呢?
发布于 2022-03-06 23:59:32
只需询问项目提供程序是否有类型为UTType.movie.identifier的项。如果是这样的话,继续通过调用loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier)加载它。
实际代码:
if prov.hasItemConformingToTypeIdentifier(UTType.movie.identifier) {
self.dealWithVideo(result)
} else if prov.canLoadObject(ofClass: PHLivePhoto.self) {
self.dealWithLivePhoto(result)
} else if prov.canLoadObject(ofClass: UIImage.self) {
self.dealWithImage(result)
}https://stackoverflow.com/questions/71374994
复制相似问题