首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

使用 QLPreviewController 显示 Documents 文件夹中的文件

QLPreviewController 是 iOS 中用于预览文件的一个框架,可以用于显示 Documents 文件夹中的文件。

在使用 QLPreviewController 前,需要先添加文件到 Documents 文件夹中,然后使用以下代码来加载文件:

代码语言:swift
复制
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 来预览文件了。

页面内容是否对你有帮助?
有帮助
没帮助

相关·内容

领券