如何将Base64数据和NSData转换为字符串

时间:2017-07-04 04:30:27

标签: ios swift base64 nsdata pdf-viewer

我是IOS的新手,我正在制作一个项目,我从Web服务接收Base64数据。如何将Base64数据转换为字符串以及如何在swift中打开pdf视图并检查是否有任何pdf所有者应用程序安装在iPhone中。我想知道如何在字符串swift中转换NSDATA。帮助我 像这样的例子是Base64数据 JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4 + CnN0cmVhbQp4nLVcza8ct5GH1rK9ejIU24lkO5GtJ8vWm5E87eY3uXtbYLHAYi8JdItySrABAjhA8v8fUuwmu35kF2fmxbsWDMxjk8VisapYX + TfbudJ6ds5 / 6S // VJ

像这样是nsdata < 25504446 2d312e34 0a25c7ec 8fa20a35 2030206f 626a0a3c 3c2f4c65 6e677468 20362030 20522f46 696c7465 72202f46 6c617465 4465636f 64653e3e 0a737472 65616d0a 789cb55c cdaf1cb7 9187d6b2 bd7a3214 db89643b 91ad27cb d69b913c

3 个答案:

答案 0 :(得分:3)

Base64DataString

let base64String = "dGhpcyBpcyBmb3IgdGVzdGluZw=="
if let data = Data(base64Encoded: base64String) {
    if let string = String(data: data, encoding: .utf8) {
        print(string)
    }
}

Base64NSDataString

let data = NSData(base64Encoded: base64String, options: .ignoreUnknownCharacters)
    var string = String(data: data, encoding: .utf8)
}

答案 1 :(得分:0)

Swift 3.0

一个方便的字符串扩展。希望能帮到你。

<% 
    if(DisplayBtn == true) { 
        out.println("<input type='image'"
        + " src='img/pencil.png'"
        + " onClick='document.sentToBack.submit()' />");
    }
%>

答案 2 :(得分:0)

这个问题分为两部分。首先,将base 64字符串转换为Data / NSData。但是你已经做到了,所以你不需要那里的帮助。

其次,将Data / NSData转换为字符串。但是,如果仔细查看该文件,您会发现该数据是PDF文件,而不是文本字符串。例如,如果我将其保存为文件并在十六进制编辑器中查看,我可以清楚地看到它是一个PDF:

hex dump

您无法将PDF二进制数据转换为字符串。 (事实上​​,这就是为什么它首先是base64编码的原因,因为它是复杂的二进制数据。)

但是,您可以使用UIDocumentInteractionController预览保存到文件的PDF文件。

例如:

// convert base 64 string to data

let base64 = "JVBERi0xLjQKJcfsj6IKNSAwIG9iago8PC9MZW5ndGggNiAwIFIvRmlsdGVyIC9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nLVcza8ct5GH1rK9ejIU24lkO5GtJ8vWm5E87eY3uXtbYLHAYi8JdItySrABAjhA8v8fUuwmu35kF2fmxbsWDMxjk8VisapYX+TfbudJ6ds5/6s/"
guard let data = Data(base64Encoded: base64) else {
    print("unable to convert base 64 string to data")
    return
}

// given the data was PDF, let's save it as such

let fileURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
    .appendingPathComponent("test.pdf")
try! data.write(to: fileURL)

// if it was a string, you could convert the data to string, but this will fail
// because the data is a PDF, not a text string
//
// guard let string = String(data: data, encoding: .utf8) else {
//     print("Unable to convert data into string")
//     return
// }
// print(string)

// So, instead, let's use `UIDocumentInteractionController` to preview the PDF:

let controller = UIDocumentInteractionController(url: fileURL)
controller.delegate = self
controller.presentPreview(animated: true)

其中,视图控制器符合UIDocumentInteractionControllerDelegate

extension ViewController: UIDocumentInteractionControllerDelegate {

    func documentInteractionControllerViewControllerForPreview(_ controller: UIDocumentInteractionController) -> UIViewController {
        return self

        // or, if this view controller is already in navigation controller, don't 
        // return `self`, like above, but instead return the navigation controller itself
        //
        // return navigationController!
    }

}