我有一个类,它有一个从CloudKit中提取数据的CKAsset(图像文件)。但是,我无法弄清楚如何初始化CKAsset。我在初始化时没有数据。该类也有字符串,但我可以使用“”来初始化它们。什么可以用来初始化CKAsset?
这是我的班级......
class Locations: NSObject, MKAnnotation {
var title: String?
var subtitle: String?
var coordinate: CLLocationCoordinate2D
var story: String?
var image: CKAsset
override init()
{
self.title = "Test Title"
self.subtitle = "Test Subtitle"
self.coordinate = CLLocationCoordinate2D.init()
self.story = ""
self.image = <- How do I init the CKAsset before I have the data?
}
}
答案 0 :(得分:0)
早上,你会在这段代码中找到你问题的答案:)
func saveLeCollection(theGlob:NSURL){
let container = CKContainer(identifier: "iCloud.com")
let publicDB = container.publicCloudDatabase
let singleLink2LinkthemALL = CKRecordID(recordName: uniqReference)
let newRecord = CKRecord(recordType: "Collection", recordID: singleLink2LinkthemALL)
let whistleAsset = CKAsset(fileURL: theAssetURL)
newRecord["theAsset"] = whistleAsset
var localChanges:[CKRecord] = []
localChanges.append(newRecord)
let saveRecordsOperation = CKModifyRecordsOperation(recordsToSave: localChanges, recordIDsToDelete: nil)
saveRecordsOperation.savePolicy = .ChangedKeys
saveRecordsOperation.perRecordCompletionBlock = { record, error in
if error != nil {
self.showAlert(message: error!.localizedDescription)
print(error!.localizedDescription)
}
// deal with conflicts
// set completionHandler of wrapper operation if it's the case
}
saveRecordsOperation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, error in
if error != nil {
self.showAlert(message: error!.localizedDescription)
print(error!.localizedDescription)
} else {
// deal with conflictsay
// set completionHandler of wrapper operation if it's the case
}
}
publicDB.addOperation(saveRecordsOperation)
}
答案 1 :(得分:0)
首先我想说在init()中设置属性就像你正在做的那样对CKAsset不起作用,因为在你打电话并将记录传递给你的类之前,你不会知道文件URL或CK将会命名保存CKAsset文件。但是,我构建了这个框架,你永远不需要在类中有一个CKAsset。它处理一切。从下载到缓存,您只需要资产的recordID和属性键。您可以直接在imageview上使用这些方法。希望这可以帮助。 https://github.com/agibson73/AGCKImage