iOS 10,QLPreview ViewController 不显示文件

标签 ios swift qlpreviewcontroller

我正在开发一个 iOS 应用程序,它向用户显示许多不同的文件。从视频和音频文件到 html 和 Office/iWorks 文件。我已经子类化了 QLPreviewController,并想用它来显示其中一些文件。我创建该对象,向其传递文件的 url,设置 ViewController 的 View 框架以替换父 View Controller 中的 webView 框架。 :

else if (QuickLookViewController.canOpenFile(previewItem: contentUrl as NSURL)) {

    hideControls()
    quickLook.contentURLs = [contentUrl as NSURL]

    //add the QuickLookController's view to content display using webview's frame
    self.view.addSubview(quickLook.view)

    quickLook.view.frame = webview!.frame
    quickLook.reloadData()

使用 View 调试层次结构工具,它显示正确设置的 View ,以及设置quickLook.backgroundColor = UIColor.black只是为了测试。

所以我认为访问下载和存储的文件可能存在问题,因此在 QuickLookController 的 viewDidLoad 中我添加了一些测试以确保文件存在:

super.viewDidLoad()
print("\nContent URL: \(contentURLs[0])")
print("\nContent URL Path: \(contentURLs[0].path!)")

self.delegate = self
self.dataSource = self
var error : NSError?

print("\nis reachable: \(contentURLs[0].checkResourceIsReachableAndReturnError(&error))")
if (error != nil) {
    print("is reachable error -> \(error.debugDescription)")
}
print("\ndoes exist: \(FileManager.default.fileExists(atPath: contentURLs[0].path!))")
print("\nCan open: \(QuickLookViewController.canOpenFile(previewItem: contentURLs[0]))")

日志语句按我的预期输出:

Content URL: file:///var/mobile/Containers/Data/Application/B9D5C288-F889-4513-941E-2564F1C12F02/Documents/588c5a1e-dffe-47a8-9824-bc19463aafc2/d88a8dd5-40d1-4fdb-adf3-10fce1f6bf1f/fd73c162-5ac3-4269-8573-9c0b61bef7a7/fd73c162-5ac3-4269-8573-9c0b61bef7a7.pages

Content URL Path: /var/mobile/Containers/Data/Application/B9D5C288-F889-4513-941E-2564F1C12F02/Documents/588c5a1e-dffe-47a8-9824-bc19463aafc2/d88a8dd5-40d1-4fdb-adf3-10fce1f6bf1f/fd73c162-5ac3-4269-8573-9c0b61bef7a7/fd73c162-5ac3-4269-8573-9c0b61bef7a7.pages

is reachable: true

does exist: true

Can open: true

我什至在viewDidLoad中使用了断点来检查quickLook的 super View 是否使用'po self.view.superview!.frame'作为llbd语句设置,并再次收到了我期望的输出。

我在应用程序中的另一个 View 堆栈中使用了同一个类,它显示了单击的文件,所以它对我来说没有多大意义。两种用途之间的唯一区别是,第二个我在以模态方式呈现的 viewController 中呈现 QuickLook 的 View 。

最佳答案

好吧,我想发布我遇到的问题的答案。问题是我没有将 QuickLook View Controller 添加到具有父子关系的 ContentDisplayViewController 中。因此,添加我想要的 View 的更新代码如下所示:

//create the parent-child relationship for the view controllers
self.addChildViewController(quickLook)

//give the view it's frame
quickLook.view.frame = webview!.frame

//add the view and call the child's didMove() method.
self.view.addSubview(quickLook.view)
quickLook.didMove(toParentViewController: self)

这个问题给了我答案: Add child view controller to current view controller

给了我一些阅读 Material 供我阅读,感谢@Piyush Patel 的帖子

关于iOS 10,QLPreview ViewController 不显示文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40592709/

相关文章:

ios - 无法使用 Process() 快速返回 Error Domain=NSPOSIXErrorDomain Code=13 "Permission denied"打开应用程序

ios - ipad中的下拉框

iOS 7 UIBarButtonItem 字体在点击时发生变化

ios - 通过Facebook SDK询问 "you have already authorize "是否正常

ios - 在肖像中,它工作正常,但在风景中,它出现在半屏上,我正在使用 wAny hAny 尺寸类别

ios - 3D Touch Peek View 不会向上滑动进行操作

ios - iOS 7 中的导航栏裁剪背景图像;ClipsToBounds :NO not working

Swift:延迟封装 map、filter、flatMap 链

iphone - 允许独立于父 View 的 QLPreviewController 旋转

ios - QLPreviewController的tintColor第一次打开时不一样