我在 NSData 中收到来自 web 服务响应的 base64String,如何快速将该 base64String 转换为 String?
I am receiving a base64String from webservice response in NSData, how to convert that base64String to String in swift?
//Code var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as! NSDictionary // Response JSON from webservice var base64String : String = "" base64String = jsonResult["Base64String"] as! String // Retrieve base64String as string from json response println("Base64String Alone: (base64String)") // Code to decode that base64String let decodedData = NSData(base64EncodedString: base64String, options: NSDataBase64DecodingOptions(rawValue: 0)) println("Decoded: (decodedData)") let decodedString = NSString(data: decodedData!, encoding: NSUTF8StringEncoding) println(decodedString) // Prints nilbase64String 的编码和解码适用于仅包含文本的文件,如果文件包含某些表格格式/图像,则编码和解码都会给出无效的 base64String.无论文件的内容如何,如何将文件转换为 base64String 编码和解码?文件格式有doc、docx、pdf、txt
Encode and decode of base64String works well for the files containing only text, if a file contains some table formats/images both encoding and decoding gives an invalid base64String. How to convert a file into base64String encode and decode whatever the contents of file ? File formats are doc, docx, pdf, txt
提前感谢您的帮助!
推荐答案试试这个
let base64Encoded = "YW55IGNhcm5hbCBwbGVhc3VyZS4=" let decodedData = Data(base64Encoded: base64Encoded)! let decodedString = String(data: decodedData, encoding: .utf8)! print(decodedString) println(decodedString)确保您的 base 64 编码字符串有效
make sure your base 64 encoded string is valid
更多推荐
如何在 Swift 中将 base64String 转换为 String?
发布评论