Я успешно загрузил PDF-файл из Интернета, и он сохранен в каталоге документов. URL-адрес загруженного файла следующий:
file:///Users/heetshah/Library/Developer/CoreSimulator/Devices/4BF83AAF-A910-46EB-AE76-91BC6BEED033/data/Containers/Data/Application/B4532805-2842-431F-B16C-C5E448C8366F/Documents/TPAFForm.pdf
Я пытаюсь отобразить его в PDFKit следующим образом.
let path = URL(fileURLWithPath: pdfUrl!)
if let document = PDFDocument(url: path) {
pdfView.document = document
pdfView.displayMode = .singlePageContinuous
pdfView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
pdfView.displaysAsBook = true
pdfView.displayDirection = .vertical
pdfView.autoScales = true
pdfView.maxScaleFactor = 4.0
pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
}
Я не получаю никакой ошибки
Я просмотрел кучу сообщений о переполнении стека, и он показывает то же решение, что и выше, но в моем случае это не работает. Я также попробовал следующее решение, но оно не работает
if let path = Bundle.main.path(forResource: pdfUrl, ofType: "pdf") {
let url = URL(fileURLWithPath: path)
if let pdfDocument = PDFDocument(url: url) {..
Ниже приведен мой код для загрузки файла
func downloadPDF(pdfUrl: String?,fileName: String,completionHandler:@escaping(String,Bool) -> ()){
let destinationPath: DownloadRequest.DownloadFileDestination = {
_,_ in
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let fileURL = documentsURL.appendingPathComponent("\(fileName).pdf")
return (fileURL,[.removePreviousFile,.createIntermediateDirectories])
}
if let pdfUrl = pdfUrl {
Alamofire.download(pdfUrl, to: destinationPath).downloadProgress { (progress) in
}.responseData { (response) in
switch response.result{
case .success:
if response.destinationURL != nil,let filePath = response.destinationURL?.absoluteString{
completionHandler(filePath,true)
}
break
case .failure:
completionHandler("Something went wrong",false)
break
}
}
}
}
Я использую Alamofire
для загрузки файла. Существуют ограничения для моего PDFView, так как я могу отображать PDF-файл с URL-адресом в своем предварительном просмотре, но мне нужно сначала загрузить PDF-файл локально, а затем отобразить его в моем PDF-представлении