QuickLook之工程文件預覽
一、簡介
QuickLook庫可以讓我們的App在iPhone/iPad中直接預覽各個文件了。官方的開發文檔中說明其支持的文件類型有:
- iWork documents
- Microsoft Office documents (Office ‘97 and newer)
- Rich Text Format (RTF) documents
- PDF files
- Images
- Text files whose uniform type identifier (UTI) conforms to the public.text type (see Uniform Type Identifiers Reference)
- Comma-separated value (csv) files
二、包含哪些類
通過api可以看到有哪些Classes
import QuickLook.QLBase//基礎類
import QuickLook.QLPreviewController //預覽Controller
import QuickLook.QLPreviewItem //單個數據源
import QuickLook.QLPreviewingController //
import QuickLook.QLThumbnailProvider
import QuickLook.QLThumbnailReply
import QuickLook.QLThumbnailRequest
三、使用demo
方式一:在自己Controller中展示文件如下:
//聲明兩個變量
fileprivate var preViewController = QLPreviewController.init()
//經過測試:可以是網絡URLStr,必須要下載下來,可以用
fileprivate var filePath = Bundle.main.path(forResource: "消息提醒", ofType: "xlsx")
//ViewDidLoad中設置變量
preViewController.delegate = self
preViewController.dataSource = self
addChild(preViewController)
view.addSubview(preViewController.view)
//佈局,可選
preViewController.view.snp.makeConstraints { (make) in
make.left.right.equalToSuperview()
make.bottom.equalToSuperview()
make.top.equalTo(topLayoutGuide.snp.bottom)
}
//遵循代理、DataSource
extension YPreviewVC: QLPreviewControllerDelegate, QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return URL.init(fileURLWithPath: filePath!) as QLPreviewItem
}
func previewControllerDidDismiss(_ controller: QLPreviewController) {
print("用戶界面已消失")
}
func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool {
return false
}
}
方式二:直接調用:
//直接調用跳轉,用QLPreviewController的默認界面,自帶系統的分享 self.navigationController?.pushViewController(preViewController, animated: true)
QuickLook之網絡URL預覽
一、把文件下載下來:Alamofire 5.0框架
方法如下:
fileprivate var filePath: URL?//聲明的屬性
fileprivate func reqestDownLoad() -> Void {
let destination = DownloadRequest.suggestedDownloadDestination(for: .documentDirectory)//這是下載存放哪裏
//自己文件名的在這裏哦
/* let destination: DownloadRequest.DownloadFileDestination = {[weak self] (url, response) in
let docmentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
let suggestFil = response.suggestedFilename == nil ? "http.xls" : response.suggestedFilename!
// response.suggestedFilename?//文件名
let newName = self!.fileName + "." + suggestFil.split(separator: ".").last!.description
let fileRUL = docmentURL?.appendingPathComponent(newName)
return (fileRUL!, [.removePreviousFile,.createIntermediateDirectories])
}*/
//urlTitle:urlString eg:http://file.xlsx
Alamofire.download(urlTitle, to: destination).validate().responseData {[weak self] response in
// case response.d
debugPrint(response)
print(response.temporaryURL)
if response.destinationURL != nil {
self!.filePath = response.destinationURL//下載完成的完整路徑
self!.preViewController.reloadData()//刷新即可展示,類似tableView
}
print(response.destinationURL)
}
}
//MARK: 代理方法
extension YPreFileVC: QLPreviewControllerDelegate, QLPreviewControllerDataSource {
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
if filePath == nil {
return 0
}
return 1 //可以使多個文件
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return filePath! as QLPreviewItem
}
func previewControllerDidDismiss(_ controller: QLPreviewController) {
print("用戶界面已消失")
}
func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool {
return false
}
}
注意
如果是網絡URL鏈接,下載下來的不想要保存到APP中可以直接在Controller視圖消失的時候直接刪除掉即可
override func viewDidDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if filePath != nil {//刪錯文件
do {
try FileManager.default.removeItem(at: filePath!)
}catch {
print(error)
print("刪錯失敗啦")
}
}
}