CKAsset 未保存到 CloudKit - 所有其他字段都已保存
CKAsset not Saved to CloudKit - All other fields are saved
我有一个 CloudKit 应用程序,它基本上是一个主细节设置
额外的功能。任何细节对象都可以标记为 ActiveNote。什么时候
该应用程序在 iPad 上,仅显示此 ActiveNote(无用户
相互作用)。该应用程序包括所有的通知和订阅
私有数据库中自定义区域中的数据。该应用程序适用于
一个例外。
只有两种记录类型。所有数据都以 CNote 类型存储。
When a detail item is chosen to be shown on the iPad, I upload that data to a single record of
输入 ActiveNote。 ActiveNote 数据仅由 iPad 用于
填充其详细视图的只读版本。 iPad动态
每当 phone 用户将记录标记为活动时更改。
所有字段都已上传并正确显示在 iPad 上
图像资产的例外。没有资产被保存,我没有收到
错误信息。普通 CNotes 的保存过程使用相同的
程序,但从我减少的相机图像开始
尺寸。这当然是一个 UIImage。我无法从此图像中保存。
如果我更改代码以加载应用程序中包含的静态 .png,则
上传工作正常。仅在尝试上传图像时
来自 DetailViewController。由于图像最初是资产
无论如何,我试图将 CKAsset 图像直接重新加载到
ActiveNote 也不起作用。
如有任何指导,我们将不胜感激。下面是保存到单个 ActiveNote 的代码。 iOS11,Xcode9.3
func queryActiveNote() {
recordZone = CKRecordZone(zoneName: "CNotes")
let query = CKQuery(recordType: "ActiveNote", predicate: NSPredicate(format: "TRUEPREDICATE"))
let sortDescriptor = NSSortDescriptor(key: "noteName", ascending: true)
query.sortDescriptors = [sortDescriptor]
privateDatabase.perform(query, inZoneWith: recordZone?.zoneID) { (results, error) in
if error != nil {
print("error querying database\(String(describing: error?.localizedDescription))")
} else {
guard results?.count > 0 else {return}
print("results?.count is \(String(describing: results?.count))")
self.activeNote = results![0]
self.activeNote!["noteName"] = self.detailItem?["noteName"]
//save a bunch of other fields too
let tmpDir = NSTemporaryDirectory()
let tmpFile = tmpDir.appending("test.png")
let tmpFileURL = URL(fileURLWithPath: tmpFile)
//Main queue to avoid runtime warning that image needs to be on main thread
DispatchQueue.main.async {
guard let tmpImage = self.imageView.image else {
print("guard let tmpImage failed")
return
}
guard let tmpImageData = UIImageJPEGRepresentation(tmpImage, 1.0) else {
print("guard let tmpImageData failed")
return
}
do {
try tmpImageData.write(to: tmpFileURL, options: .atomic)
print("the write of chosenImageData succeeded")
} catch {
print("error writing chosenImageData to a file")
}//do catch
let asset : CKAsset = CKAsset(fileURL: tmpFileURL)
self.activeNote!["noteImageData"] = asset
}//main
self.privateDatabase.save(self.activeNote!, completionHandler: { (record, error) in
if error != nil {
print("privateDatabase error saving activeNote: \(String(describing: error?.localizedDescription))")
} else {
print("modified activeNote record saved successfully")
DispatchQueue.main.async {
let ac = UIAlertController(title: nil , message: "Record was successfully saved as Active", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
ac.addAction(okAction)
self.present(ac, animated: true, completion: nil)
}//main
}//if error else
})//save block
}//if error else
}//perform query block
}//queryActiveNote
user3069232 应该得到答案。上面的代码确实保存
在图像文件保存完成之前记录到 CloudKit。我搬家了
do catch 块中的 CloudKit save 块,过程是
成功的。因为我保证在所有记录中都有图像(那里
是默认值)然后使 CloudKit 保存有条件
图片保存成功即可。
我有一个 CloudKit 应用程序,它基本上是一个主细节设置 额外的功能。任何细节对象都可以标记为 ActiveNote。什么时候 该应用程序在 iPad 上,仅显示此 ActiveNote(无用户 相互作用)。该应用程序包括所有的通知和订阅 私有数据库中自定义区域中的数据。该应用程序适用于 一个例外。
只有两种记录类型。所有数据都以 CNote 类型存储。 When a detail item is chosen to be shown on the iPad, I upload that data to a single record of 输入 ActiveNote。 ActiveNote 数据仅由 iPad 用于 填充其详细视图的只读版本。 iPad动态 每当 phone 用户将记录标记为活动时更改。
所有字段都已上传并正确显示在 iPad 上 图像资产的例外。没有资产被保存,我没有收到 错误信息。普通 CNotes 的保存过程使用相同的 程序,但从我减少的相机图像开始 尺寸。这当然是一个 UIImage。我无法从此图像中保存。 如果我更改代码以加载应用程序中包含的静态 .png,则 上传工作正常。仅在尝试上传图像时 来自 DetailViewController。由于图像最初是资产 无论如何,我试图将 CKAsset 图像直接重新加载到 ActiveNote 也不起作用。
如有任何指导,我们将不胜感激。下面是保存到单个 ActiveNote 的代码。 iOS11,Xcode9.3
func queryActiveNote() {
recordZone = CKRecordZone(zoneName: "CNotes")
let query = CKQuery(recordType: "ActiveNote", predicate: NSPredicate(format: "TRUEPREDICATE"))
let sortDescriptor = NSSortDescriptor(key: "noteName", ascending: true)
query.sortDescriptors = [sortDescriptor]
privateDatabase.perform(query, inZoneWith: recordZone?.zoneID) { (results, error) in
if error != nil {
print("error querying database\(String(describing: error?.localizedDescription))")
} else {
guard results?.count > 0 else {return}
print("results?.count is \(String(describing: results?.count))")
self.activeNote = results![0]
self.activeNote!["noteName"] = self.detailItem?["noteName"]
//save a bunch of other fields too
let tmpDir = NSTemporaryDirectory()
let tmpFile = tmpDir.appending("test.png")
let tmpFileURL = URL(fileURLWithPath: tmpFile)
//Main queue to avoid runtime warning that image needs to be on main thread
DispatchQueue.main.async {
guard let tmpImage = self.imageView.image else {
print("guard let tmpImage failed")
return
}
guard let tmpImageData = UIImageJPEGRepresentation(tmpImage, 1.0) else {
print("guard let tmpImageData failed")
return
}
do {
try tmpImageData.write(to: tmpFileURL, options: .atomic)
print("the write of chosenImageData succeeded")
} catch {
print("error writing chosenImageData to a file")
}//do catch
let asset : CKAsset = CKAsset(fileURL: tmpFileURL)
self.activeNote!["noteImageData"] = asset
}//main
self.privateDatabase.save(self.activeNote!, completionHandler: { (record, error) in
if error != nil {
print("privateDatabase error saving activeNote: \(String(describing: error?.localizedDescription))")
} else {
print("modified activeNote record saved successfully")
DispatchQueue.main.async {
let ac = UIAlertController(title: nil , message: "Record was successfully saved as Active", preferredStyle: .alert)
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
ac.addAction(okAction)
self.present(ac, animated: true, completion: nil)
}//main
}//if error else
})//save block
}//if error else
}//perform query block
}//queryActiveNote
user3069232 应该得到答案。上面的代码确实保存 在图像文件保存完成之前记录到 CloudKit。我搬家了 do catch 块中的 CloudKit save 块,过程是 成功的。因为我保证在所有记录中都有图像(那里 是默认值)然后使 CloudKit 保存有条件 图片保存成功即可。