我正在创建UIImage生成的PDF并将其保存到设备中。我也在Core Data中保存该文件的路径。我可以检索路径并检查文件是否存在,但是当我尝试在UIWebView中加载PDF时,屏幕返回空白。我已经尝试加载请求以及加载数据,但没有一个接近的工作。我将此代码应用于TableView didSelectRow函数,因此当用户单击该单元格时,会出现一个可重用的弹出窗口,该弹出窗口将加载PDF。以下是我的代码。
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedDoc = tableView.cellForRow(at: indexPath) as! DisplayMainSavedDataCell
let document : String = selectedDoc.documentNameLabel.text!
print(document)
let path = fetchDocumentURL(documentName: document)
let urlDoc = URL(fileURLWithPath: path)
print(urlDoc)
let request = URLRequest.init(url: urlDoc)
let sb = UIStoryboard.init(name: "Main", bundle: nil)
let popup = sb.instantiateViewController(withIdentifier: "pdfPopUp") as! PDFViewController
popup.webView?.loadRequest(request)
present(popup, animated: true)
}
func fetchDocumentURL (documentName: String) -> String {
let docName = documentName
let fetchRequest : NSFetchRequest<Document> = Document.fetchRequest()
let predicate = NSPredicate(format: "docName == %@", docName as CVarArg)
fetchRequest.predicate = predicate
do {
let results = try PersistenceService.context.fetch(fetchRequest)
for result in results as [Document] {
let path = result.docPath
return path!
}
} catch{
print("files does not exists or file missing")
let path = "No path exists"
return path
}
let path = "No path exists"
return path
}