2017-08-09 5 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

オーディオオブジェクトをデータのまとまりとして保存します。または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からブロブをデコードします。

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

が明らかに再びこの例では、画像データを扱っているが、あなたと私はそのすべてのデータ:)それが何色がない母校を知っています。

ここでの唯一のCAVETは、私はかなり確信して資産があなたの実行の - ミルのiCloudオブジェクトに別のフォレストに保管されています、速度、およびそれらへのアクセスは少し遅くなることができます。

+0

Ryan、この回答があなたに役立つかどうか教えてください。緑色のボックスにチェックを入れる:) – user3069232

関連する問題