我将我的 pdfData 交给用户保存。他可以保存到文件并制作文件,但pdf文件的默认名称是:PDF document.pdf
。如果可能的话,我想要我自己的文件名。也许我可以在将 pdfData 提供给之前更改 pdfData 中的文件名UIActivityViewController
?
这是我的代码:
// Create page rect
let pageRect = CGRect(x: 0, y: 0, width: 595.28, height: 841.89) // A4, 72 dpi
// Create PDF context and draw
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, pageRect, nil)
UIGraphicsBeginPDFPage()
// From here you can draw page, best make it in a function
PdfErstellung.PdfErstellen(auswahlZeilen, vitalstoffWerteListe, heuteString)
UIGraphicsEndPDFContext()
// Save pdf DATA through user
let activityViewController = UIActivityViewController(activityItems: [pdfData], applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // für IPAD nötig
self.present(activityViewController, animated: true, completion: nil)
- 更新 -
我的新想法是,首先尝试保存文件并尝试 URL,如果失败,则直接使用 pdfData,因为在某些模拟器中使用 URL 不会给出错误,而在其他模拟器中会给出错误。
更多这里:https://stackoverflow.com/a/52499637/10392572 https://stackoverflow.com/a/52499637/10392572
您只需将 pdfData 保存到临时 fileURL 并共享该 URL。
let temporaryFolder = FileManager.default.temporaryDirectory
let fileName = "document.pdf"
let temporaryFileURL = temporaryFolder.appendingPathComponent(fileName)
print(temporaryFileURL.path) // /Users/lsd/Library/Developer/XCPGDevices/E2003834-07AB-4833-B206-843DC0A52967/data/Containers/Data/Application/322D1F1D-4C97-474C-9040-FE5E740D38CF/tmp/document.pdf
do {
try pdfData.write(to: temporaryFileURL)
// your code
let activityViewController = UIActivityViewController(activityItems: [temporaryFileURL], applicationActivities: nil)
} catch {
print(error)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)