且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在iOS + Swift中将音频转换为Base64字符串

更新时间:2023-01-18 18:56:54

您必须从文件位置获取NSData,在这里您丢失了该位置.检查以下代码:

You have to get NSData from the location of your file, which is you missing here. Check below code:

var error: NSError?

var fileLocation = NSString(string:NSBundle.mainBundle().pathForResource("test", ofType: "mp3")!)
let fileData = NSData(contentsOfFile: fileLocation, options: NSDataReadingOptions.DataReadingMappedIfSafe, error: &error)

let base64String = fileData?.base64EncodedStringWithOptions(.allZeros)
println(base64String!)

在这里您可以获取音频文件的NSData(无论文件格式是什么),然后将这些数据转换为base64.

Here you can get NSData of audio file (whatever file format is) and then convert this data to base64.