gpt4 book ai didi

swift - iOS/swift : QLPreviewController shows blank page in iOS11

转载 作者:搜寻专家 更新时间:2023-11-01 07:07:37 28 4
gpt4 key购买 nike

我尝试在我的 iOS 项目中使用 QLPreviewController 进行文件(.txt、.pdf、.docx、.xlsx 等)预览,但它在 iOS 11 中运行不佳:无论使用什么设备,它都会显示空白页面或模拟器。在 iOS 10.3.1(模拟器)中运行良好,一切正常。

详细信息如下:

环境:

Xcode version: 8.3.3, 9.0.1
Device: iPhone 7 Plus, Simulator
iOS version: 11.0.3(iPhone 7 Plus), 10.3.1(Simulator), 11.0.1(Simulator)
Swift version: 3.1/3.2

代码

import UIKit
import QuickLook

class QuickViewController: UIViewController {

var url: String!
var filePath: URL!
var msg: Message! {
didSet {
url = msg.content
// create document folder url
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
// let tempDirectory = URL(fileURLWithPath: NSTemporaryDirectory())
filePath = documentsURL.appendingPathComponent((msg.content as NSString).lastPathComponent)
}
}

@IBOutlet weak var contentView: UIView!


// MARK: - Initialisation

override func viewDidLoad() {
super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
loafFile()
}


func loafFile() {
if FileManager.default.fileExists(atPath: filePath.path) {
quickLookFile()
} else {
AppManager_Inst.download(url: "\(url!)", destinationUrl: filePath, completion: { [unowned self](succeed, msg) in
if succeed {
self.quickLookFile()
} else {
print(msg)
}
})
}
}

func quickLookFile() {

print(filePath.path)

/// Trying to read the file via FileManager to check if the filePath is correct or not
let data = FileManager.default.contents(atPath: filePath.path)
print(data)

if QLPreviewController.canPreview(filePath as NSURL) {
let QLController = QLPreviewController()
QLController.delegate = self
QLController.dataSource = self
QLController.view.frame = self.contentView.frame
self.view.addSubview(QLController.view)
} else {
print("file cannot to preview")
}
}

}


extension QuickViewController: QLPreviewControllerDataSource, QLPreviewControllerDelegate {

// QLPreviewControllerDataSource

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return filePath as NSURL
}

// QLPreviewControllerDelegate
func previewController(_ controller: QLPreviewController, shouldOpen url: URL, for item: QLPreviewItem) -> Bool {
return true
}

}

我不确定我遗漏了什么或这里有什么问题,请帮忙。

p.s.: 稍后我会在 iPhone 5c(iOS 10.3.1) 上尝试代码,完成后结果应该会更新到这里。

最佳答案

您没有将当前 viewController 添加为 QLPreviewController() 的父类

QLController.didMove(toParentViewController: self)添加到self.view

后即可

我认为这应该可以解决您的问题。

关于swift - iOS/swift : QLPreviewController shows blank page in iOS11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47053192/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com