作者:小白秋波_314 | 来源:互联网 | 2023-05-17 09:12
IhavesuccessfullysavedanAudiofiletoParsebutamstrugglingtodownloaditagain.Icantwor
I have successfully saved an Audio file to Parse but am struggling to download it again. I can't work out what the block should be to go with getDataInBackgroundWithBlock. And how to actually save the file. Any help much appreciated!
我已经成功地保存了一个音频文件来解析,但是我还在努力下载它。我无法计算出该块应该与getDataInBackgroundWithBlock一起使用。以及如何保存文件。任何帮助表示感谢!
let query = PFQuery(className: "wordRecordings")
query.whereKey("wordName", equalTo: "\(joinedWord)")
query.findObjectsInBackgroundWithBlock { (objects:[PFObject]?, error:NSError?) -> Void in
if error == nil{
for object in objects! {
let audioFile = object["audioFile"] as! PFFile
audioFile.getDataInBackgroundWithBlock{ (audioFile: NSData, error:NSError?) -> Void in // THIS BLOCK ISNT CORRECT...
if error == nil {
let audioFile = AudioFile(data: audioFile) // THIS WORKS FOR IMAGES BUT NOT AUDIO - HOW DO I CREATE AN AUDIO FILE
}
}
}
} else {
print("Error: \(error!) \(error!.userInfo)")
}
}
Thanks to @David below here is a working solution with path code as well.
感谢下面的@David,这里还有一个带有路径代码的有效解决方案。
let fileString = "\(fileName)"
let documentsDirectory = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0]
let path = documentsDirectory.URLByAppendingPathComponent(fileString)
audioFile.getDataInBackgroundWithBlock { (audioFile:NSData?, error:NSError?) -> Void in
if error == nil {
audioFile.writeToURL(path, atomically: true)
} else {
print("error with getData")
}
}
1 个解决方案