代码:
import QuickLook let openFileVC = OpenFileViewController(fullFilePath:fullFilePath) // self.showViewController(openFileVC, sender: self) let previewController = QLPreviewController() previewController.delegate = openFileVC previewController.dataSource = openFileVC self.showViewController(previewController, sender: self) 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 { return self.fileUrl } override func viewDidLoad() { super.viewDidLoad() } } |
出错:
This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release. 2016-01-19 11:39:51.976 JianDao[22569:1282566] Cannot find preview item for proxy: <QLPreviewItemProxy: 0x7d8a3a90> – 张炳恋-设计.docx (0) respData==readOutData = true 2016-01-19 11:39:58.906 JianDao[22569:1282674] *** Assertion failure in -[UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UIApplication.m:1712 2016-01-19 11:39:58.922 JianDao[22569:1282674] *** Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘accessing _cachedSystemAnimationFence requires the main thread’ *** First throw call stack: ( 0 CoreFoundation 0x0140da14 __exceptionPreprocess + 180 1 libobjc.A.dylib 0x0379de02 objc_exception_throw + 50 2 CoreFoundation 0x0140d8aa +[NSException raise:format:arguments:] + 138 3 Foundation 0x01a4fd26 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 118 4 UIKit 0x022c2dc0 -[UIApplication _cachedSystemAnimationFenceCreatingIfNecessary:] + 212 5 UIKit 0x022c2ea6 -[UIApplication _systemAnimationFenceCreatingIfNecessary:] + 43 6 UIKit 0x02358891 +[UIWindow _synchronizedDrawingFence] + 117 7 UIKit 0x02bd8e16 -[_UIRemoteViewController synchronizeAnimationsInActions:] + 129 8 UIKit 0x02bdcc66 -[_UIRemoteViewController viewWillAppear:] + 1179 9 QuickLook 0x0215b8e1 -[QLPreviewController viewWillAppear:] + 198 10 UIKit 0x0249fd37 -[UIViewController _setViewAppearState:isAnimating:] + 659 11 UIKit 0x024a0427 -[UIViewController __viewWillAppear:] + 147 12 UIKit 0x024c35b9 -[UIViewController(UIContainerViewControllerProtectedMethods) beginAppearanceTransition:animated:] + 202 13 UIKit 0x024d59cc -[UINavigationController _startCustomTransition:] + 1389 14 UIKit 0x024e7769 -[UINavigationController _startDeferredTransitionIfNeeded:] + 803 15 UIKit 0x024e8ada -[UINavigationController __viewWillLayoutSubviews] + 68 16 UIKit 0x026c4c4a -[UILayoutContainerView layoutSubviews] + 252 17 UIKit 0x0fb56d8a -[UILayoutContainerViewAccessibility layoutSubviews] + 50 18 UIKit 0x0239a008 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 810 19 libobjc.A.dylib 0x037b2059 -[NSObject performSelector:withObject:] + 70 20 QuartzCore 0x0204180a -[CALayer layoutSublayers] + 144 21 QuartzCore 0x020354ee _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 388 22 QuartzCore 0x02035352 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 26 23 QuartzCore 0x02027e8b _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 317 24 QuartzCore 0x0205be03 _ZN2CA11Transaction6commitEv + 561 25 QuartzCore 0x0205c1ab _ZN2CA11Transaction14release_threadEPv + 289 26 libsystem_pthread.dylib 0x047ebf54 _pthread_tsd_cleanup + 537 27 libsystem_pthread.dylib 0x047ebad9 _pthread_exit + 108 28 libsystem_pthread.dylib 0x047ec536 pthread_get_stackaddr_np + 0 29 libsystem_pthread.dylib 0x047e9f72 start_wqthread + 34 ) libc++abi.dylib: terminating with uncaught exception of type NSException |
如图:
改为:
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) }) |
至少可以显示视图了
虽然内容是空白:
去确认,此处要打开的文件是正常的文件。
有去打开别的文件,结果也是空白:
[基本解决]用QLPreviewController打开预览文件但是显示空白
转载请注明:在路上 » [已解决]swift中代码运行出错:This application is modifying the autolayout engine from a background thread