如何预览iOS11 Swift 4中来自URL的ppt文档

时间:2018-08-16 06:11:05

标签: ios swift quicklook

我正在尝试预览来自远程URL的pdf和ppt文档,并且正在使用QuickLookPreviewController预览文档。到目前为止,我已经能够预览pdf和视频,但是当我打开ppt时,它会打开quickLookController,而不是ppt预览,我什么都没看到

我不知道这是否是QLPreviewController的问题,或者我需要做一些额外的事情才能打开它。我在下面发布了我的代码,以便您了解问题所在。

 import UIKit
 import Alamofire
 import QuickLook

 class AchievementsDetailViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,QLPreviewControllerDataSource {

 let appDelegate = UIApplication.shared.delegate as! AppDelegate
 var fileURLs = [URL]()
 let quickLookController = QLPreviewController()

override func viewDidLoad() {
    super.viewDidLoad()
    self.title = "Achievements View"
    quickLookController.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    getAllAchievementsDetails()
}
func extractAndBreakFilenameInComponents(fileURL: NSURL) -> (fileName: String, fileExtension: String) {

    let fileURLParts = fileURL.path!.components(separatedBy: "/")
    let fileName = fileURLParts.last
    let filenameParts = fileName?.components(separatedBy: ".")

    return (filenameParts![0], filenameParts![1])
}

func getAllAchievementsDetails() {

fileURLs = []
let detailAchievementsURL = "\(WebAPI.baseURL + 
WebAPI.detailAchievementAPI)\(appDelegate.userId)\(WebAPI.achievementId + id)"
    Alamofire.request(detailAchievementsURL, method: .get, parameters: nil, encoding: JSONEncoding.default, headers: nil).responseJSON {response in
        switch response.result {
        case .success:
            let resposneString = response.result.value as! String
            guard let detailAchievementData = Universal.convertStringToDictionary(text: resposneString) else {return}
            switch response.response?.statusCode {
            case 200?:
                let imagesList = detailAchievementData["lstimages"] as! [[String : Any]]
                for imageData in imagesList {
                    guard let imagePath = imageData["imagesurl"] else {return}
                    let imageString = "\(WebAPI.mediaBaseURL)\(imagePath)"
                    let image = imageString.replacingOccurrences(of: "~", with:"")
                    guard let imageURL = URL(string: image) else {return}
                    self.fileURLs.append(imageURL)
                }
                let videosList = detailAchievementData["lstvideos"] as! [[String : Any]]
                for videoData in videosList {
                    guard let videoPath = videoData["videossurl"] else {return}
                    let videoString = "\(WebAPI.mediaBaseURL)\(videoPath)"
                    let video = videoString.replacingOccurrences(of: "~", with: "")
                    guard let videoURL = URL(string: video) else {return}
                    self.fileURLs.append(videoURL)
                }
                let docsList = detailAchievementData["lstdocuments"] as! [[String : Any]]
                for documentData in docsList {
                    guard let documentPath = documentData["DocumentPath"] else {return}
                    let documentString = "\(WebAPI.mediaBaseURL)\(documentPath)"
                    let document = documentString.replacingOccurrences(of: "~", with: "")
                    guard let escapedAddress = document.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed) else {return}
                    guard let documentURL = URL(string: escapedAddress) else {return}
                    self.fileURLs.append(documentURL)
                }
            case 500?:
                print(resposneString)
            default:
                print(resposneString)
            }                
        case .failure(let error):
                print(error.localizedDescription)
        }
        self.achievementsCollectionView.reloadData()
    }

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

    return fileURLs.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "mediaCell", for: indexPath) as! AchievementDetailCell
    return cell
}

func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
    return fileURLs.count
}

func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
    return fileURLs[index] as QLPreviewItem
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    let currentFileParts = extractAndBreakFilenameInComponents(fileURL: fileURLs[indexPath.row] as NSURL)
    if currentFileParts.fileExtension == "pdf" || currentFileParts.fileExtension == "ppt" {
        if QLPreviewController.canPreview(fileURLs[indexPath.row] as QLPreviewItem) {
            quickLookController.currentPreviewItemIndex = indexPath.row
            navigationController?.pushViewController(quickLookController, animated: true)
        }
    }
}

我希望我的代码一切都很好...如果没有,请指导我。.预先感谢

0 个答案:

没有答案