简体   繁体   English

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

[英]Swift: How to handle video and photo results from a PHPicker?

I need the ability for a user to select multiple photos and videos from there photo library.我需要用户能够从那里的照片库中获取 select 多张照片和视频。 (Using PHPicker) I already know how to get images with this: (使用 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()
}

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.我假设我需要做的是检查 PHPickerResult 的每个结果是什么数据类型,然后相应地加载每个 object。 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?问题是视频文件可能是 .mov、.mp4 等。如何将 PHPickerResult 识别为视频并进行相应处理?

Just ask whether the item provider has an item of type UTType.movie.identifier .只需询问项目提供者是否有UTType.movie.identifier类型的项目。 If so, go ahead and load it by calling loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier) .如果是这样,请提前 go 并通过调用loadFileRepresentation(forTypeIdentifier: UTType.movie.identifier)加载它。

Actual code:实际代码:

    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)
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM