【发布时间】:2017-03-01 12:47:06
【问题描述】:
我有一个用例需要将 UIImage 保存到文档目录 - 然后需要将 UIImage 转换为 PDF 并保存到文档目录。
转换为 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 仍然在文档目录中未触及(请参阅应用容器下面的图像以获得证明),FileManager 说 PNG 不存在?
问题
为什么FileManager 会忽略显而易见的事实并阻止我的应用程序运行?
【问题讨论】:
-
打印路径并与预期的路径进行比较。
-
我做到了。它们是相同的。
-
我遇到了完全相同的问题!寻找答案!
-
@HanXu 我回答了我自己的问题。看看这对你有没有帮助。
-
@BrandonA,太好了!有用!谢了!
标签: ios nsfilemanager uigraphicscontext