struct VideoPicker: UIViewControllerRepresentable { @Environment(.presentationMode) private var presentationMode let sourceType: UIImagePickerController.SourceType // let onImagePicked: (UIImage) -> Void let onURLPicked: (URL) -> Void final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate { @Binding private var presentationMode: PresentationMode private let sourceType: UIImagePickerController.SourceType private let onURLPicked: (URL) -> Void init(presentationMode: Binding<PresentationMode>, sourceType: UIImagePickerController.SourceType, onURLPicked: @escaping (URL) -> Void) { presentationMode = presentationMode self.sourceType = sourceType self.onURLPicked = onURLPicked } func imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { // let uiImage = info[UIImagePickerController.InfoKey.originalImage] as! UIImage // onImagePicked(uiImage) if let url = info[.mediaURL] as? URL{ onURLPicked(url) } presentationMode.dismiss() } func getVideoURL(from assetIdentifier: String, completion: @escaping (URL?) -> Void) { let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil) guard let asset = fetchResult.firstObject else { completion(nil) return } let options = PHVideoRequestOptions() options.version = .original PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { avAsset, , _ in guard let avAsset = avAsset else { completion(nil) return } let url = (avAsset as? AVURLAsset)?.url completion(url) } } func imagePickerControllerDidCancel( picker: UIImagePickerController) { presentationMode.dismiss() } } func makeCoordinator() -> Coordinator { return Coordinator(presentationMode: presentationMode, sourceType: sourceType, onURLPicked: onURLPicked) } func makeUIViewController(context: UIViewControllerRepresentableContext<VideoPicker>) -> UIImagePickerController { let picker = UIImagePickerController() picker.sourceType = sourceType picker.delegate = context.coordinator picker.mediaTypes = ["public.movie"] return picker } func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<VideoPicker>) { } }获取的url内的路径不可用
时间: 2023-11-22 12:55:00 浏览: 150
获取的 URL 可能是一个相册中的资源标识符,需要通过 PHAsset 和 PHImageManager 来获取视频的实际 URL。可以在 `Coordinator` 类中添加一个方法来获取视频的 URL,然后在 `imagePickerController(_:didFinishPickingMediaWithInfo:)` 方法中调用该方法来获取实际的 URL。
具体实现如下:
```swift
final class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
// ...
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let assetIdentifier = info[.phAsset] as? String {
getVideoURL(from: assetIdentifier) { url in
if let url = url {
onURLPicked(url)
}
}
}
presentationMode.dismiss()
}
func getVideoURL(from assetIdentifier: String, completion: @escaping (URL?) -> Void) {
let fetchResult = PHAsset.fetchAssets(withLocalIdentifiers: [assetIdentifier], options: nil)
guard let asset = fetchResult.firstObject else {
completion(nil)
return
}
let options = PHVideoRequestOptions()
options.version = .original
PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { avAsset, _, _ in
guard let avAsset = avAsset else {
completion(nil)
return
}
let url = (avAsset as? AVURLAsset)?.url
completion(url)
}
}
// ...
}
```
这样获取到的 URL 就是实际的视频 URL,可以进行使用了。
阅读全文