1

我有一个类,它有一个从 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?
}
}
4

2 回答 2

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)


}
于 2016-03-22T08:16:49.827 回答
0

首先,我会说像您正在做的那样在 init() 中设置属性不适用于 CKAsset,因为在您拨打电话并将记录传递给您的类之前,您不会知道 CK 将保存 CKAsset 的文件URL或名称文件。但是,我构建了这个框架,你永远不需要在课堂上拥有 CKAsset。它处理一切。从下载到缓存,您只需要记录 ID 和资产的属性键。您可以直接在 imageview 上使用这些方法。希望这可以帮助。https://github.com/agibson73/AGCKImage

于 2016-03-22T12:12:31.477 回答