我正在设置一个应用程序,该应用程序可以集中接收文档/文件的URL和名称(https://something.com/model.obj f.e)和“模型名称”。一旦收到它,我就使用Alamofire下载它,然后将其推送到所需的ViewController。但是,下载后,将不会显示该文件。
我尝试将下载逻辑移出视图之外,尝试将视图添加为一个子视图,但是问题仍然存在。
Origin View Controller
func prepareQuickLookSegue(_ url: URL,_ name: String,_ viewController: UIViewController) {
SVProgressHUD.show()
let destination: DownloadRequest.DownloadFileDestination = { _, _ in
var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
documentsURL.appendPathComponent(url.lastPathComponent)
return (documentsURL, [.removePreviousFile])
}
Alamofire.download(url, to: destination).responseData { response in
if let destinationUrl = response.destinationURL {
SVProgressHUD.dismiss()
let vc = QuickLookViewController(url: destinationUrl, name: name)
viewController.navigationController?.pushViewController(vc, animated: true)
}
}
}
快速查看视图控制器
import UIKit
import QuickLook
class QuickLookViewController: UIViewController, QLPreviewControllerDataSource, QLPreviewControllerDelegate {
let quickLookController = QLPreviewController()
var name:String?
var fileURL:NSURL?
init(url:URL,name:String){
self.fileURL = url as NSURL
self.name = name
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
quickLookController.dataSource = self
quickLookController.delegate = self
navigationItem.title = self.name
}
func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
return 1
}
func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
return fileURL!
}
func previewController(controller: QLPreviewController, shouldOpenURL url: NSURL, forPreviewItem item: QLPreviewItem) -> Bool {
if item as? NSURL == fileURL {
return true
} else {
print("Will not open URL \(String(describing: url.absoluteString))")
}
return false
}
}
我希望显示图像/文件,但显示黑色图像。
答案 0 :(得分:0)
问题在于您的QuickLookViewController从未真正呈现快速外观。您有一个QLPreviewController,但它只是坐在那里,未使用。
由于QLPreviewController本身是一个视图控制器,所以建议不要使用QuickLookViewController,而只从第一个视图控制器显示QLPreviewController。