如何将图像添加到存储在变量中的 QuickLook
Posted
技术标签:
【中文标题】如何将图像添加到存储在变量中的 QuickLook【英文标题】:How to add image to QuickLook that is stored in variable 【发布时间】:2020-05-17 10:12:51 【问题描述】:我想将几个存储在变量中的图像添加到 QLPreviewController。 QuickLook 数据源需要一个 fileURL,我不确定如何获取存储在变量中而不是磁盘或项目中的图像。
关于如何解决这个问题的任何指示?
【问题讨论】:
你有什么尝试吗?您需要将图像保存到本地存储并获取路径并将其传递给QLPreviewController
我真的不知道如何正确使用它。我希望能找到一个例子,以此作为我理解的基础。
【参考方案1】:
以下代码可用于显示来自本地和来自QLPreviewController
中的 URL 的图像文件。
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
【讨论】:
有没有办法对存储在变量中的图像使用快速查看? 你的意思是,存储在变量中的图像?图像存储在本地项目中?这边走? let image = UIImage(named: "test.png") 感谢您的回答。我的意思是从互联网上下载并存储在变量中的图像 是的,您可以将这些图像拖到项目中并引用这些图像并从上面的代码中调用 displayLocalFile() 方法。以上是关于如何将图像添加到存储在变量中的 QuickLook的主要内容,如果未能解决你的问题,请参考以下文章