当检查视频的 mediaUrl 是否存在于 swift 中的照片库时,FileManager 总是给出 false
我有一个条件,保存从iPhone照片库中选择的视频路径。 ImagePicker控制器用于保存媒体Url。我已成功选择视频并将路径保存到用户默认值,但稍后无法识别照片库中是否存在相同的媒体文件。我正在添加下面的代码。
下面的类是从照片库中选择任何视频并通过视图模型功能保存路径。
class LinkVideoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func openGallery() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .photoLibrary
myPickerController.mediaTypes = ["public.movie"]
myPickerController.videoQuality = .typeHigh
self.present(myPickerController, animated: true, completion: nil)
}
}
}
extension LinkVideoViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
let urlString = videoUrl.relativePath
print(FileManager.default.fileExists(atPath: videoUrl.relativePath))
viewModel.selectVideo(selectedVideoPath: urlString)
self.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
在下面的类中,我们检测照片库中是否存在相同的文件。
override func start() {
if let videoPath = UserDefaultValues.videoGuidedPath, !videoPath.isEmpty, let url = URL(string: videoPath) {
if FileManager.default.fileExists(atPath: url.relativePath) {
// if file exist then play video
} else {
// show video does not exist error view
}
} else {
// if not path is saved then probably user has not selected, so give an option for request permission
}
}
我已经检查过,值仍然相同 FileManager 文件存在函数每次都给出错误值。我不明白其背后的原因。 我也使用路径代替相对路径,但错误是相同的。
I have a condition to save the video path selected from the iPhone photo library. ImagePicker Controller is used to save media Url. I have successfully selected the video and saved the path to User defaults but when failed to Identify later if the same media file exists in the Photo library or not. I am adding the code below.
The below class is to select any video from the Photo library and saved the path via the View Model function.
class LinkVideoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
func openGallery() {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .photoLibrary
myPickerController.mediaTypes = ["public.movie"]
myPickerController.videoQuality = .typeHigh
self.present(myPickerController, animated: true, completion: nil)
}
}
}
extension LinkVideoViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
if let videoUrl = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
let urlString = videoUrl.relativePath
print(FileManager.default.fileExists(atPath: videoUrl.relativePath))
viewModel.selectVideo(selectedVideoPath: urlString)
self.dismiss(animated: true, completion: nil)
}
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.dismiss(animated: true, completion: nil)
}
}
Below class where we detect if the same file exists in the Photo library or not.
override func start() {
if let videoPath = UserDefaultValues.videoGuidedPath, !videoPath.isEmpty, let url = URL(string: videoPath) {
if FileManager.default.fileExists(atPath: url.relativePath) {
// if file exist then play video
} else {
// show video does not exist error view
}
} else {
// if not path is saved then probably user has not selected, so give an option for request permission
}
}
I have checked, value is same still FileManager file Exist function gives a false value every time. I did not get the reason behind it.
I have used path also in place of relative Path but the error is the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
文件管理器有时可能很棘手,
file manager can be tricky some times,