当检查视频的 mediaUrl 是否存在于 swift 中的照片库时,FileManager 总是给出 false

发布于 2025-01-16 01:07:15 字数 2147 浏览 5 评论 0原文

我有一个条件,保存从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冬天旳寂寞 2025-01-23 01:07:15

文件管理器有时可能很棘手,

 FileManager.default.fileExists(atPath: "file:///Users/Mac/Library/Developer/CoreSimulator/Devices/A1AA0615-4AE4-4710-9E15-A6973BFFA9B8/data/Media/DCIM/100APPLE/IMG_0007.MP4") 
 // return false

 FileManager.default.fileExists(atPath: "Users/Mac/Library/Developer/CoreSimulator/Devices/A1AA0615-4AE4-4710-9E15-A6973BFFA9B8/data/Media/DCIM/100APPLE/IMG_0007.MP4") 
 // return true

file manager can be tricky some times,

 FileManager.default.fileExists(atPath: "file:///Users/Mac/Library/Developer/CoreSimulator/Devices/A1AA0615-4AE4-4710-9E15-A6973BFFA9B8/data/Media/DCIM/100APPLE/IMG_0007.MP4") 
 // return false

 FileManager.default.fileExists(atPath: "Users/Mac/Library/Developer/CoreSimulator/Devices/A1AA0615-4AE4-4710-9E15-A6973BFFA9B8/data/Media/DCIM/100APPLE/IMG_0007.MP4") 
 // return true
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文