Swift:如何处理 PHPicker 的视频和照片结果?

2023-12-10

我需要用户能够从照片库中选择多张照片和视频。 (使用 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 识别为视频并进行相应处理?


只需询问物品提供商是否有该类型的物品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)
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swift:如何处理 PHPicker 的视频和照片结果? 的相关文章

随机推荐