2017-08-09 54 views
1

我已經創建了一個應用程序,使用Swift 3和Xcode 8.3.3來記錄音頻文件並將它們保存到應用程序的Document目錄中。我現在想將這些文件保存到iCloud以備份它們。我已經能夠使用這個代碼,以一個簡單的記錄保存到iCloud:如何使用Swift將音頻文件保存到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是否能夠處理該對象。有什麼建議麼?

回答

1

使用iOS 10.x Swift 3.0

您可以將audioObject保存爲blob數據;或在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像這樣snipit碼解碼你的斑點。

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

顯然再次這個例子中處理圖像數據,但你我都知道它的所有數據:)沒有母校它是什麼顏色。

這裏唯一的警告就是速度,我非常確定資產被保存在一個不同的森林中,而不是普通的iCloud對象,訪問它們可能會慢一些。

+0

瑞安,讓我知道這個答案適合你嗎?勾選綠色框:) – user3069232

相關問題