我試圖選擇資產PHPickerViewController作為我的替代品UIImagePickerController,但是在設定filter和config選項時,我沒有看到設定回傳視頻長度限制的方法。即人們可能已經使用UIImagePickerControllerthrough 的方式videoMaximumDuration。
有沒有人找到任何形式的鍛煉?
private func presentPicker(filter: PHPickerFilter?)-> PHPickerViewController {
var configuration = PHPickerConfiguration(photoLibrary: .shared())
// Set the filter type according to the user’s selection.
configuration.filter = filter
// Set the mode to avoid transcoding, if possible, if your app supports arbitrary image/video encodings.
configuration.preferredAssetRepresentationMode = .current
// Set the selection behavior to respect the user’s selection order.
configuration.selection = .ordered
// Set the selection limit to enable singular selection.
configuration.selectionLimit = 1
// Set the preselected asset identifiers with the identifiers that the app tracks.
configuration.preselectedAssetIdentifiers = []
let picker = PHPickerViewController(configuration: configuration)
//picker.delegate = self
//present(picker, animated: true)
return picker
}
uj5u.com熱心網友回復:
在看看的檔案PHPickerFilter和PHPickerViewController我無法通過視頻長度看到任何原生的方式來過濾。
我能想到的最好的解決方案是符合委托PHPickerViewControllerDelegate并在用戶選擇它后檢查視頻長度。
您還沒有提供其余的代碼,但您應該遵守PHPickerViewControllerDelegate并實作所需的方法:
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) {
guard let provider = results.first?.itemProvider else { return }
provider.loadItem(forTypeIdentifier: UTType.movie.identifier, options: [:]) { (videoURL, error) in
// Check the video length here
}
}
}
有關如何加載視頻的示例,請參閱上面的代碼。從這里您可以按照一些現有的答案來查看如何提取視頻時長。如果視頻持續時間在您要過濾的范圍內,請將其復制到適當的位置FileManager并使用您認為合適的方式對其進行處理,如果不是(即它太長或太短,不符合您的喜好),然后丟棄它并使用警報/訊息告訴用戶。
這是有關如何獲取視頻時長的示例。注意:您只需要將 url 的參考替換為videoURL您在完成處理程式中有權訪問的(請參閱評論)。
獲取視頻的準確時長
另一種解決方案是使用來自第 3 方的更強大的照片選擇器。
這樣做的缺點是要求用戶為其整個照片庫提供權限(因為照片選擇器不需要提示權限,因為它是 iOS 提供的沙盒視圖控制器)。它還為您的應用程式引入了另一個依賴項。
如果這些缺點適合您的用例,那么這可能正是您要尋找的。
這是一個提供一些更強大過濾器的庫:https : //github.com/Yummypets/YPImagePicker
請參閱自述檔案中的“視頻”部分。它允許您指定最小和最大視頻持續時間。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/396581.html
