gpt4 book ai didi

ios - 如何使用 Swift 将音频文件保存到 iCloud?

转载 作者:可可西里 更新时间:2023-11-01 02:01:59 27 4
gpt4 key购买 nike

我使用 Swift 3 和 Xcode 8.3.3 创建了一个应用程序,用于录制音频文件并将它们保存到应用程序的文档目录中。我现在想将这些文件保存到 iCloud 以备份它们。我已经能够使用以下代码将简单的记录保存到 iCloud:

let database = CKContainer.default().publicCloudDatabase

func saveToCloud(myContent: String){
let myRecord = CKRecord(recordType: "AudioRecording")
myRecord.setValue(myContent, forKey: "content")
database.save(myRecord) { (record, error) in
print(error ?? "No error")
guard record != nil else {return}
print("Saved record to iCloud")
}
}

看来我只需要添加一行代码,看起来像这样:

newNote.setValue(audioObject, forKey: "Audio")

但我不确定我需要为 audioObject 传递什么对象,以及 iCloud 是否能够处理该对象。有什么建议吗?

最佳答案

使用 iOS 10.x Swift 3.0

您可以将 audioObject 保存为数据 block ;或者在 iCloud 中, Assets 。这是保存图像的一些基本代码,但原理相同,只是一个数据 block 。

这里的代码比您真正需要的要多得多,但我将其保留在上下文中。

func files_saveImage(imageUUID2Save: String) {
var localChanges:[CKRecord] = []
let image2updated = sharedDataAccess.image2Cloud[imageUUID2Save]

let newRecordID = CKRecordID(recordName: imageUUID2Save)
let newRecord = CKRecord(recordType: "Image", recordID: newRecordID)

let theLinkID = CKReference(recordID: sharedDataAccess.iCloudID, action: .deleteSelf)
let thePath = sharedDataAccess.fnGet(index2seek: sharedDataAccess.currentSN)
newRecord["theLink"] = theLinkID
newRecord["theImageNo"] = image2updated?.imageI as CKRecordValue?
newRecord["theImagePath"] = sharedDataAccess.fnGet(index2seek: image2updated?.imageS as! Int) as CKRecordValue?
newRecord["theUUID"] = imageUUID2Save as CKRecordValue?

let theURL = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(NSUUID().uuidString+".dat")
do {
try image2updated?.imageD.write(to: theURL!)
} catch let e as NSError {
print("Error! \(e)");
return
}

newRecord["theImageBlob"] = CKAsset(fileURL: URL(string: (theURL?.absoluteString)!)!)

localChanges.append(newRecord)
let records2Erase:[CKRecordID] = []

let saveRecordsOperation = CKModifyRecordsOperation(recordsToSave: localChanges, recordIDsToDelete: records2Erase)
saveRecordsOperation.savePolicy = .changedKeys
saveRecordsOperation.perRecordCompletionBlock = { record, error in
if error != nil {
print(error!.localizedDescription)
}
// deal with conflicts
// set completionHandler of wrapper operation if it's the case
}
saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
self.theApp.isNetworkActivityIndicatorVisible = false
if error != nil {
print(error!.localizedDescription, error!)
} else {
print("ok")
}
}

saveRecordsOperation.qualityOfService = .background
privateDB.add(saveRecordsOperation)
theApp.isNetworkActivityIndicatorVisible = true
}

当你想反过来时,你可以使用像这样的代码从 iCloud 解码你的 blob。

 let imageAsset = record["theImageBlob"] as? CKAsset
if let _ = imageAsset {
if let data = NSData(contentsOf: (imageAsset?.fileURL)!) {
imageObject = data
}
}

显然这个例子再次处理图像数据,但你我都知道它的所有数据 :) 不管它是什么颜色。

这里唯一需要注意的是速度,我很确定 Assets 与您的普通 iCloud 对象保存在不同的林中,访问它们可能会稍微慢一些。

关于ios - 如何使用 Swift 将音频文件保存到 iCloud?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45597152/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com