使用 Apple 的 QuickLook 框架快速预览本地和外部文件及其内容。
此软件包提供了对 Apple QuickLook Framework 的封装。
在 Xcode 中使用其 Github 仓库 URL 添加此 Swift 包。(文件 > Swift Packages > Add Package Dependency...)
初始化 View (SwiftUI) 或 ViewController (UIKit)
/// Initializes the `SBQuickLookView` or `SBQuickViewController` with the given file items and configuration.
///
/// - Parameters:
/// - fileItems: The `[SBQLFileItem]` data for populating the preview. Could be one or many items.
/// - configuration: Optional `SBQLConfiguration` configurations.
/// - completion: Optional `Result<SBQLError?, SBQLError>` completion.
/// - success: `QLPreviewController` successfully presented with at least one item. Optional `SBQLError` if some items failed to download.
/// - failure: `QLPreviewController` could not be presented.
public init(
fileItems: [SBQLFileItem],
configuration: SBQLConfiguration? = nil,
completion: ((Result<SBQLError?, SBQLError>) -> Void)? = nil) {
self.fileItems = fileItems
self.configuration = configuration
self.completion = completion
}
您必须将文件项 SBFileItem
传递给初始化器。可以传递一个或多个项目到 View (SwiftUI) 或 ViewController (UIKit)。
/// Initializes the file item with the given values.
///
/// - Parameters:
/// - url: `URL` of the item, choose between local file URLs or external URLs
/// - title: Optional title `String` to be displayed in the QuickLook controller.
/// - content: Optional media type `String` of the file; e.g. `"pdf"`, `"jpeg"`, ...
/// - urlRequest: Optional `URLRequest` used to download the item. The `url` is always set to `fileItem.url`. Default: `URLRequest(url: fileItem.url)`
public init(url: URL, title: String? = nil, mediaType: String? = nil, urlRequest: URLRequest? = nil) {
self.url = url
self.title = title
self.mediaType = mediaType
self.urlRequest = urlRequest
}
您还可以将可选的 SBQLConfiguration
传递给初始化器。
/// Initializes the file item with the given values.
///
/// - Parameters:
/// - urlRequest: Optional `URLSession`. Overrides the `URLSession` used by the download task. Default: `URLSession.shared`
/// - localFileDir: Optional local directory `URL`. Overrides the local directory `URL` used by the download task. Default: `FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask)`
public init(session: URLSession? = nil, localFileDir: URL? = nil) {
self.session = session
self.localFileDir = localFileDir
}
从您喜欢的 View 中呈现 SBQuickLookView
(它是 SBQuickViewController
的 UIViewControllerRepresentable
封装器)。
@State var isShown = false
let fileItems: [SBFileItem]
let configuration: SBQLConfiguration
init() {
let localFileURL = Bundle.main.url(forResource: "sample-local-pdf", withExtension: "pdf")!
fileItems = [
SBFileItem(url: localFileURL, title: "LOCAL FILE"),
SBFileItem(url: URL(string: "https://file-examples.com/storage/fe197d899c63f609e194cb1/2017/10/file_example_PNG_500kB.png")!, title: "Nice PNG Image", mediaType: "png")
]
let localFileDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
configuration = SBQLConfiguration(localFileDir: localFileDir)
}
var body: some View {
VStack {
Button {
isShown.toggle()
} label: {
Text("Open QuickLook of files")
}
}
.fullScreenCover(isPresented: $isShown, content: {
SBQuickLookView(fileItems: fileItems, configuration: configuration) { result in
switch result {
case .success(let downloadError):
if let downloadError {
print(downloadError)
}
case .failure(let error):
switch error.type {
case .emptyFileItems:
print("emptyFileItems")
case .qlPreviewControllerError:
print("qlPreviewControllerError")
case .download(let errorFileItems):
print("all items failed downloading")
print(errorFileItems)
}
}
}
})
}
从您喜欢的 ViewController 中呈现 SBQuickViewController
。
let localFileURL = Bundle.main.url(forResource: "sample-local-pdf", withExtension: "pdf")!
let fileItems = [
SBFileItem(url: localFileURL, title: "LOCAL FILE"),
SBFileItem(url: URL(string: "https://file-examples.com/storage/fe197d899c63f609e194cb1/2017/10/file_example_PNG_500kB.png")!, title: "Nice PNG Image", mediaType: "png")
]
let localFileDir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let configuration = SBQLConfiguration(localFileDir: localFileDir)
let qlController = SBQuickViewController(fileItems: fileItems, configuration: configuration)
qlController.modalPresentationStyle = .overFullScreen
present(qlController, animated: true)
示例 SwiftUI 项目可以在这里找到。