QLPreviewController 是 iOS 中用于预览文件的一个框架,可以用于显示 Documents 文件夹中的文件。
在使用 QLPreviewController 前,需要先添加文件到 Documents 文件夹中,然后使用以下代码来加载文件:
let fileUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileUrl = fileUrl.appendingPathComponent("file.pdf")
do {
try FileManager.default.copyItem(at: URL(fileURLWithPath: NSHomeDirectory()), to: fileUrl)
try QLPreviewController().show(from: fileUrl, in: self)
} catch {
print(error.localizedDescription)
}
其中,FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! 获取了 Documents 文件夹的 URL,然后使用 appendingPathComponent 方法将文件名添加到 URL 中,生成完整的文件 URL。最后使用 QLPreviewController 的 show 方法来加载文件。
这样就可以在 App 的 Documents 文件夹中直接使用 QLPreviewController 来预览文件了。
领取专属 10元无门槛券
手把手带您无忧上云