【问题标题】:How do I save an audio file to iCloud using Swift?如何使用 Swift 将音频文件保存到 iCloud?
【发布时间】:2018-01-17 17:24:43
【问题描述】:

我使用 Swift 3 和 Xcode 8.3.3 创建了一个应用程序,它记录音频文件并将它们保存到应用程序的 Document 目录中。我现在想将这些文件保存到 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 swift xcode icloud cloudkit


    【解决方案1】:

    使用 iOS 10.x Swift 3.0

    您可以将您的音频对象保存为数据块;或者在 iCloud 中,一种资产。下面是一些保存图片的基本代码,不过原理是一样的,就是一团数据。

    这里的代码比你真正需要的要多,但我把它留在了上下文中。

    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
                        }
                    }
    

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

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

    【讨论】:

    • Ryan,让我知道这个答案是否适合您?勾选绿色框:)
    猜你喜欢
    • 2018-12-29
    • 2022-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多