如何在Swift中显示来自NSData的PDF-如何将PDF保存到文档文件夹Swift-如何在Swift中通过WebView显示来自已保存NSData的PDF [英] How to show PDF from NSData in Swift - how to save PDF to documents folder Swift - how to display PDF from saved NSData via WebView in Swift

查看:153
本文介绍了如何在Swift中显示来自NSData的PDF-如何将PDF保存到文档文件夹Swift-如何在Swift中通过WebView显示来自已保存NSData的PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要从NSData形式的服务器中提取的PDF,现在我需要显示它.我一直在寻找解决方案,但还没有找到可以弥补原始NSData与实际保存或显示PDF之间的鸿沟的任何方法.

I have a PDF that I'm pulling from a sever that came in the form of NSData and now I need to display it. I've looked far and wide for a solution to this and I haven't found anything that bridges the gap between raw NSData and actually saving or showing the PDF.

我尝试了此操作,但现在我卡住了,不知道如何保存或显示:

I tried this but now I'm stuck not knowing how to save or show:

let cfData = CFDataCreate(kCFAllocatorDefault, UnsafePointer<UInt8>(data.bytes), data.length)                                                
let cgDataProvider = CGDataProviderCreateWithCFData(cfData)
let cgPDFDocument = CGPDFDocumentCreateWithProvider(cgDataProvider)

推荐答案

首先,NSData IS 是您的PDF,无需转换为其他数据类型或使用库对其进行操作此时此刻.

First of all, that NSData IS your PDF, no need to convert to another data type or use a library to manipulate it at the moment.

现在,只需将NSData保存到iOS上的documents目录中,该目录不是像CoreData或Realm这样的托管持久性存储,也不是UserDefaults.这是一个有东西的文件夹.

For now, simply save the NSData to your documents directory on iOS, which is not a managed persistent store like CoreData or Realm, nor is it UserDefaults. It's a folder with stuff in it.

保存到文档文件夹:

let data = //the stuff from your web request or other method of getting pdf    
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let filePath = "\(documentsPath)/myCoolPDF.pdf"
data.writeToFile(filePath, atomically: true)

现在,请验证文件是否存在,然后在Mac上将其打开.这是为了确保您已经保存了实际的PDF,而不是其他任何东西.这是一个两步过程.首先通过从iOS模拟器打印documents文件夹的位置来找出文件到底在哪里:

Now verify that the file is there and open it on your mac. This is to make sure you've saved an actual PDF and not anything else. This is a two step process. First find out where on earth the file went by printing the location of the documents folder from the iOS simulator:

let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
print(documentsDirectory)

现在复制该非常长的文件路径并在终端中复制cd [paste location]到那里,然后在预览中打开open myCoolPDF.pdf来打开它!这是神奇的时代!

Now copy that really long filepath and cd [paste location] in terminal to go there, then open myCoolPDF.pdf to open it in Preview! It's magical times!

现在您已经确认您正在处理实际的PDF,现在可以在WebView中显示它了,因为它非常简单,除非您有自己的方法.

Now that you have verified that you're dealing with an actual PDF it's time to display that in a WebView since it's quite simple unless you've got your own way of doing so.

请注意,您仍然必须这样做,以便显示Web视图,将其拖到情节提要中的viewController上并为其创建IBOutlet.

Note that you still have to make it so the webview shows up, drag one onto your viewController in a storyboard and make an IBOutlet for it.

let url = NSURL(fileURLWithPath: filePath)
let webView = UIWebView()
webView.loadRequest(NSURLRequest(URL: url))

显然,这是确保您掌握基础知识的一种快速方法,您不想使用!不安全地强制打开包装.

Obviously this is a quick way to make sure you get the basics going, you don't want to force unwrap unsafely using !.

这篇关于如何在Swift中显示来自NSData的PDF-如何将PDF保存到文档文件夹Swift-如何在Swift中通过WebView显示来自已保存NSData的PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆