使用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對象,訪問它們可能會慢一些。
瑞安,讓我知道這個答案適合你嗎?勾選綠色框:) – user3069232