如何在QuickLook中添加存储在变量中的图片?

问题描述 投票:-1回答:1

我想在QLPreviewController中添加几张存储在变量中的图片,QuickLook数据源需要一个fileURL,我不知道如何获取存储在变量中而不是磁盘或项目中的图片。QuickLook的数据源需要一个fileURL,我不知道如何获取存储在变量中而不是磁盘或项目中的图片。

有什么建议可以帮助我解决这个问题吗?

ios swift uiimage quicklook qlpreviewcontroller
1个回答
0
投票

下面的代码可以用来显示本地和URL中的图片文件。QLPreviewController.

import UIKit
import QuickLook

class ViewController: UIViewController {

    lazy var previewItem = NSURL()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func displayLocalFile(_ sender: UIButton){

        let previewController = QLPreviewController()
        // Set the preview item to display
        self.previewItem = self.getPreviewItem(withName: "bull.png")

        previewController.dataSource = self
        self.present(previewController, animated: true, completion: nil)

    }

    @IBAction func displayFileFromUrl(_ sender: UIButton){

        // Download file
        self.downloadfile(completion: {(success, fileLocationURL) in

            if success {
                // Set the preview item to display======
                self.previewItem = fileLocationURL! as NSURL
                // Display file

                DispatchQueue.main.async {
                   let previewController = QLPreviewController()
                    previewController.dataSource = self
                    self.present(previewController, animated: true, completion: nil)
                }


            }else{
                debugPrint("File can't be downloaded")
            }
        })
    }



    func getPreviewItem(withName name: String) -> NSURL{

        //  Code to diplay file from the app bundle
        let file = name.components(separatedBy: ".")
        let path = Bundle.main.path(forResource: file.first!, ofType: file.last!)
        let url = NSURL(fileURLWithPath: path!)

        return url
    }

    func downloadfile(completion: @escaping (_ success: Bool,_ fileLocation: URL?) -> Void){

        let itemUrl = URL(string: "https://developer.apple.com/wwdc20/images/hero/memoji/large/L7_2x.jpg")

        // then lets create your document folder url
        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        // lets create your destination file url
        let destinationUrl = documentsDirectoryURL.appendingPathComponent("filename.jpg")

        // to check if it exists before downloading it
        if FileManager.default.fileExists(atPath: destinationUrl.path) {
            debugPrint("The file already exists at path")
            completion(true, destinationUrl)

            // if the file doesn't exist
        } else {

            // you can use NSURLSession.sharedSession to download the data asynchronously
            URLSession.shared.downloadTask(with: itemUrl!, completionHandler: { (location, response, error) -> Void in
                guard let tempLocation = location, error == nil else { return }
                do {
                    // after downloading your file you need to move it to your destination url
                    try FileManager.default.moveItem(at: tempLocation, to: destinationUrl)
                    print("File moved to documents folder")
                    completion(true, destinationUrl)
                } catch let error as NSError {
                    print(error.localizedDescription)
                    completion(false, nil)
                }
            }).resume()
        }
    }

}

//MARK:- QLPreviewController Datasource

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

    func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {

        return self.previewItem as QLPreviewItem
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.