Ios 使用alamofire上传pdf文件

Ios 使用alamofire上传pdf文件,ios,swift,swagger,Ios,Swift,Swagger,我从swagger code gen创建了以下函数: open class func uploadFile(firstname: String, lastname: String, timestamp: Date, file: URL, completion: @escaping ((_ data: ApiResponse?,_ error: Error?) -> Void)) { 在应用程序中,您可以使用相机制作图像,并将图像转换为pdf文件: let document = PDFD

我从swagger code gen创建了以下函数:

 open class func uploadFile(firstname: String, lastname: String, timestamp: Date, file: URL, completion: @escaping ((_ data: ApiResponse?,_ error: Error?) -> Void)) {
在应用程序中,您可以使用相机制作图像,并将图像转换为pdf文件:

let document = PDFDocument()
let pdfPage = PDFPage(image: unwrapImage)
document.insert(pdfPage!,at: 0)

现在我想上传这个文件。但是
document.documentURL
总是为零。虽然我可以在显示器上显示pdf文档。我是否应该将pdf文档保存到临时目录以使用带有url参数的函数?

PDFDocument的属性
documentURL
仅为get。如果不使用url初始值设定项,它将始终返回nil。您需要的是将您的pdf文档
dataRepresentation
write
pdf数据写入临时或永久位置的url。然后你可以上传它的URL

let document = PDFDocument()
let image = UIImage(named: "imageName.jpg")!
if let pdfPage = PDFPage(image: image) {
    document.insert(pdfPage,at: 0)
    do {
        print(FileManager.default.temporaryDirectory.path)
        let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent("pdfName.pdf")
        try document.dataRepresentation()?.write(to: fileURL)
        // upload your fileURL or copy to a permanent location
    } catch {
        print(error)
    }
}