FileManager说文件不存在 [英] FileManager says file doesn't exist when it does

查看:330
本文介绍了FileManager说文件不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,要求将UIImage保存到文档目录-然后需要将UIImage转换为PDF并保存到文档目录.

I have a use case that requires a UIImage to be saved to the documents directory - then the UIImage needs to be converted into a PDF and saved to the documents directory.

要转换为PDF的代码

var filePath = NSString(string: self.selectedMedia!.imagePath!)

if FileManager().fileExists(atPath: filePath as String) {

       filePath = filePath.deletingPathExtension as NSString
       filePath = NSString(string: filePath.appendingPathExtension("PDF")!)

       if let image = UIImage(data: self.selectedMedia?.image! as! Data) {

           let rect:CGRect = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)

           UIGraphicsBeginPDFContextToFile(filePath as String, rect, nil);
           UIGraphicsBeginPDFPageWithInfo(rect, nil);
           UIGraphicsGetCurrentContext();
           image.draw(in: rect)
           UIGraphicsEndPDFContext();

           self.selectedMedia?.imagePath = filePath as String

           CoreData.save()
       }

       self.showPDFEditor()
   }
else {
   print("Missing File At Path \(filePath)")
} 

问题

FileManager表示文件不存在.第一次执行该文件时,之前,我们创建PDF,FileManager可以检测到该图像PDF确实存在.但是之后,我们从图像创建了PDF-即使图像PNG仍未更改在documents目录中(请参见应用容器的下面的图像以证明),FileManager说PNG不存在吗?

FileManager says that a file does not exist when it does. The first time this is executed, before we create the PDF, FileManager can detect that image PDF does indeed exist. But After we create the PDF from the image - even though the image PNG is still in the documents directory untouched (see image below of app container for proof), FileManager says the PNG does not exist?

问题

为什么FileManager忽略明显的事实并阻止我的应用程序正常运行?

Why does FileManager ignore the obvious truth and keep my application from functioning?

推荐答案

从不存储完整的文件路径

我的PDF和图像位于iOS文件系统中的Documents目录下.每次您运行该应用程序时,iOS都会将root path更改为documents目录.因此,像上面一样保存imagePath效果很好,直到我重新启动应用程序.这是因为我将 entire 路径存储到我的selectedMedia对象.

Never store the complete file path

My PDF and Images were residing under the Documents directory within the iOS file system. iOS will change the root path to the documents directory every time that you run the application. So saving an imagePath as I was above would work just fine, until I restarted the app. That is because I was storing the entire path to my selectedMedia object.

查看Application/someLongNumber/有何不同.这就是为什么当您检查文件是否在文档目录中并且实际上是否存在于文件目录中,但根路径已更改的原因.

See how the Application/someLongNumber/ is different. This is why when you check if a file is in your documents directory and in fact is there, but the root path has changed.

不是存储整个文件路径,而是存储来自documents目录的 子路径.因此,我在文档目录中创建了一个名为Media的子文件夹,将文件保存至该子文件夹,并且我存储在对象上的imagePath的路径为Media/theFileName.PNG.

Instead of storing the entire file path just store the subpath from the documents directory. So what I did was created a subfolder within the documents directory called Media that I save the file to and the path I store to my imagePath on my objects would be Media/theFileName.PNG.

因此,您需要做的是在运行时生成/Documents目录的路径.我不能足够强调这部分.这样可以确保您每次都可以访问实际的文档目录.查看 SO答案,以获取有关如何生成文档目录路径的提示.

So what you need to do is generate the path to the /Documents directory at run time. I can not stress this part enough. This will guarantee you are accessing the actual documents directory each time. Check out this SO answer for tips on how to generate the path to documents directory.

拥有运行时文档目录的路径后,只需将存储的路径添加到该路径的末尾,它应如下所示:

After you have the path to the run time documents directory just add your stored path to the end of that and it should look something like this:

这篇关于FileManager说文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆