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.
CODE TO CONVERT TO 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)")
}
ISSUE
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?
QUESTION
Why does FileManager
ignore the obvious truth and keep my application from functioning?