我怎样才能使用base64string的PDF文件? 迅速

我正在寻找一种方法来保存我的项目目录中的PDF文件,我从Web服务中收到了一个64位的pdfstring。 我必须将其转换为NSData或类似的东西?

我是Swift编码新手,但我可以按照你的指示。

我希望你能帮助我。 谢谢

是的,您必须将其转换为数据,然后将其保存到设备上的文档目录中。 像这样的function将工作:

func saveBase64StringToPDF(_ base64String: String) { guard var documentsURL = (FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)).last, let convertedData = Data(base64Encoded: base64String) else { //handle error when getting documents URL return } //name your file however you prefer documentsURL.appendPathComponent("yourFileName.pdf") do { try convertedData.write(to: documentsURL) } catch { //handle write error here } //if you want to get a quick output of where your //file was saved from the simulator on your machine //just print the documentsURL and go there in Finder print(documentsURL) } 

你想要的东西像:

 if let d = Data(base64Encoded: base64string) { do { try d.write(to: outputFile) } catch { // handle error } } 

其中base64string是base64 PDFstring, outputFile是PDF输出文件的URL。