折腾:
期间,遇到:
使用:
let fullFilePath = fullUrl!.path! print("fullFilePath=\(fullFilePath)") //fullFilePath=/Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C9EAE23F-BCF3-4C2E-9D90-DD282CE8FBB2/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/张炳恋–设计.docx dispatch_async(dispatch_get_main_queue(), { () -> Void in // let openFileVC = OpenFileViewController(fileUrl: NSURL(fileURLWithPath: self.fileItem.downloadedFilename, relativeToURL: docDirUrl)) // let openFileVC = OpenFileViewController(fileUrl:fullUrl!) let openFileVC = OpenFileViewController(fullFilePath:fullFilePath) // self.showViewController(openFileVC, sender: self) let previewController = QLPreviewController() previewController.delegate = openFileVC previewController.dataSource = openFileVC self.showViewController(previewController, sender: self) }) // // OpenFileViewController.swift // JianDao // // Created by licrifan on 16/1/19. // Copyright © 2016年 licrifan. All rights reserved. // import UIKit import QuickLook class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate{ //class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, UIDocumentInteractionControllerDelegate{ // let dic:UIDocumentInteractionController let fileUrl:NSURL // init(fileUrl:NSURL){ init(fullFilePath:String){ self.fileUrl = NSURL.fileURLWithPath(fullFilePath) print("self.fileUrl=\(self.fileUrl)") //self.fileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C9EAE23F-BCF3-4C2E-9D90-DD282CE8FBB2/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx // self.dic = UIDocumentInteractionController(URL: self.fileUrl) super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int { return 1 } func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem { //file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/5BCAF5A9-4508-43E7-AC3D-E3DF1D811C88/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E6%9D%8E%E5%BB%BA%E4%BA%AE-%E7%AE%80%E9%81%93.pptx return self.fileUrl } override func viewDidLoad() { super.viewDidLoad() } } |
但是显示是空白:
搜:
qlpreviewcontroller blank
去加上:
previewController.modalPresentationStyle = UIModalPresentationStyle.FullScreen |
问题依旧。
Design matters: FIX: A blank view appears after QLPreviewController dismissed in iOS 7
UIModalPresentationStyle.CurrentContext:不行
UIModalPresentationStyle.Custom:不行
blank QLPreviewController in iOS application – Stack Overflow
Quicklook/QLPreviewController shows a blank page instead of pdf on ios 8 – Xamarin Forums
改为:
dispatch_async(dispatch_get_main_queue(), { () -> Void in // let openFileVC = OpenFileViewController(fileUrl: NSURL(fileURLWithPath: self.fileItem.downloadedFilename, relativeToURL: docDirUrl)) // let openFileVC = OpenFileViewController(fileUrl:fullUrl!) let openFileVC = OpenFileViewController(fullFilePath:fullFilePath) // self.showViewController(openFileVC, sender: self) let previewController = QLPreviewController() previewController.delegate = openFileVC previewController.dataSource = openFileVC previewController.modalPresentationStyle = UIModalPresentationStyle.Custom //self.showViewController(previewController, sender: self) self.presentViewController(previewController, animated: true, completion: nil) }) |
结果是:
改为:
let openFileNvc = UINavigationController() openFileNvc.view.backgroundColor = UIColor.whiteColor() openFileNvc.navigationBarHidden = true self.presentViewController(openFileNvc, animated: true, completion: nil) openFileNvc.pushViewController(previewController, animated: false) |
结果还是空白:
QLPreviewController only shows a gray screen wi… | Apple Developer Forums
qlpreviewcontroller blank iOS9
objective c – ios 9 QLPreviewController done button is not showing – Stack Overflow
Developer Forums: Blank form view that I can’t dismiss
let openFileVC = OpenFileViewController(fullFilePath:fullFilePath) // self.showViewController(openFileVC, sender: self) let previewController = QLPreviewController() previewController.delegate = openFileVC previewController.dataSource = openFileVC previewController.transitioningDelegate = openFileVC previewController.modalPresentationStyle = UIModalPresentationStyle.Custom self.showViewController(previewController, sender: self) import QuickLook class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate, UIViewControllerTransitioningDelegate{ } |
问题依旧。
// previewController.transitioningDelegate = openFileVC // previewController.modalPresentationStyle = UIModalPresentationStyle.Custom previewController.modalPresentationStyle = UIModalPresentationStyle.FormSheet |
问题依旧。
QLPreviewController 白屏 iOS9
qlpreviewcontroller black screen
QLPreviewController and iOS7: black screen – Stack Overflow
QuickLook/QLPreviewController(QLPreviewItem) change title in iOS8 in iOS7 works fine
QLPreviewController shows blank page in iOS8 inside UINavigationController, but not independently
代码改为:
let openFileVC = OpenFileViewController(fullFilePath:fullFilePath) self.showViewController(openFileVC, sender: self) class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate{ let fileUrl:NSURL init(fullFilePath:String){ self.fileUrl = NSURL.fileURLWithPath(fullFilePath) print("self.fileUrl=\(self.fileUrl)") super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int { return 1 } func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem { //file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/5BCAF5A9-4508-43E7-AC3D-E3DF1D811C88/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E6%9D%8E%E5%BB%BA%E4%BA%AE-%E7%AE%80%E9%81%93.pptx return self.fileUrl } override func viewDidLoad() { super.viewDidLoad() print("self.view.frame=\(self.view.frame)") self.view.frame = CGRectMake( self.view.frame.origin.x, self.view.frame.origin.y, UIScreen.mainScreen().bounds.width, UIScreen.mainScreen().bounds.height //667 – statusBarFrame.height //20 – HeightNaviBar) //44, Note: can not use naviBarView.frame.height for naviBarView.frame is 0 print("self.view.frame=\(self.view.frame)") let previewController = QLPreviewController() previewController.delegate = self previewController.dataSource = self previewController.modalPresentationStyle = UIModalPresentationStyle.FullScreen self.showViewController(previewController, sender: self) } } |
至少出现一点界面了:
但是内容还是不显示,还是白屏。
改为:
let openFileVC = OpenFileViewController(fullFilePath:fullFilePath) self.showViewController(openFileVC, sender: self) // // OpenFileViewController.swift // JianDao // // Created by licrifan on 16/1/19. // Copyright © 2016年 licrifan. All rights reserved. // import UIKit import QuickLook class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate, UIViewControllerTransitioningDelegate{ //class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate{ //class OpenFileViewController: UIViewController, QLPreviewControllerDataSource, UIDocumentInteractionControllerDelegate{ // let dic:UIDocumentInteractionController let fileUrl:NSURL // init(fileUrl:NSURL){ init(fullFilePath:String){ self.fileUrl = NSURL.fileURLWithPath(fullFilePath) print("self.fileUrl=\(self.fileUrl)") //self.fileUrl=file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/C9EAE23F-BCF3-4C2E-9D90-DD282CE8FBB2/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E5%BC%A0%E7%82%B3%E6%81%8B-%E8%AE%BE%E8%AE%A1.docx // self.dic = UIDocumentInteractionController(URL: self.fileUrl) super.init(nibName: nil, bundle: nil) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func numberOfPreviewItemsInPreviewController(controller: QLPreviewController) -> Int { return 1 } func previewController(controller: QLPreviewController, previewItemAtIndex index: Int) -> QLPreviewItem { //file:///Users/crifan/Library/Developer/CoreSimulator/Devices/84FE8057-ED19-4703-BAE0-7C5156289534/data/Containers/Data/Application/5BCAF5A9-4508-43E7-AC3D-E3DF1D811C88/Documents/user-e85906ff-aca6-457f-af46-793e46b51c71/%E6%9D%8E%E5%BB%BA%E4%BA%AE-%E7%AE%80%E9%81%93.pptx return self.fileUrl } override func viewDidLoad() { super.viewDidLoad() let previewController = QLPreviewController() previewController.delegate = self previewController.dataSource = self // previewController.modalPresentationStyle = UIModalPresentationStyle.FullScreen previewController.transitioningDelegate = self previewController.modalPresentationStyle = UIModalPresentationStyle.Custom self.showViewController(previewController, sender: self) } } |
终于可以显示内容了:
预览图片:
点击右上角,还可以调用其他程序打开:
压缩文件:
然后预览打开其他文件,也是可以的:
双击可以放大: