最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[基本解决]用QLPreviewController打开预览文件但是显示空白

Swift crifan 6381浏览 0评论

折腾:

[记录]swift调用iOS内置程序打开某个文件

期间,遇到:

使用:

                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

Quicklook/QLPreviewController shows a blank page instead of pdf on ios 8 but works fine on iOS7 – Stack Overflow

iOS 8 Release Notes

objective c – Quicklook/QLPreviewController shows a blank page. Gives a warning about framework symbols not found – Stack Overflow

ios – QLPreviewController shows blank page in iOS8 inside UINavigationController, but not independently – Stack Overflow

去加上:

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)

结果还是空白:

Developer Forums: QLPreviewController View is Black when Embedded and Presented Modally in a Navigation Controller (iOS 8 b2)

QLPreviewController显示空白页iOS8UINavigationController内部,而不是独立 – QLPreviewController shows blank page in iOS8 inside UINavigationController, but not independentlyIT怪 挨踢怪 28印象 二八印象

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)
    }
}

终于可以显示内容了:

预览图片:

点击右上角,还可以调用其他程序打开:

压缩文件:

然后预览打开其他文件,也是可以的:

双击可以放大:

[基本解决]如何取消掉QLPreviewController调用时多增加的一个无用的导航界面

转载请注明:在路上 » [基本解决]用QLPreviewController打开预览文件但是显示空白

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.231 seconds, using 22.23MB memory