ios - 将 Assets 列表(数组)保存到特定的 CKRecord
<p><p>我在 CloudKit 后端创建了一个 CKRecord 类型,其中包含与该类相关的一些属性。</p>
<p>我有 String 属性、Bytes 和 Asset List 属性,所以存储一些图像(与单个记录相关的多个图像)。</p>
<p> <a href="/image/50jDc.png" rel="noreferrer noopener nofollow"><img src="/image/50jDc.png" alt="Asset List"/></a> </p>
<p>现在我正在尝试存储一些图像,然后填充属性,然后尝试将其保存到 CloudKit,但它不起作用。</p>
<p>代码如下:</p>
<pre><code>var images_array = ()
// append the an image to the array
images_array.append(CKAsset(fileURL: writeImage(image: selectedImage) as URL))
let record = CKRecord(recordType: recordName)
record["class_title"] = someString as CKRecordValue
record["class_body"]= someString as CKRecordValue
record["images_array"] = images_array as CKRecordValue
saveRecord(record)
func saveRecord(_ xrecord: CKRecord) {
let publicData = CKContainer.default().publicCloudDatabase
let record: =
let saveOperation = CKModifyRecordsOperation.init(recordsToSave: record, recordIDsToDelete: nil)
saveOperation.perRecordCompletionBlock = {(record, error) -> Void in
if (error != nil) {
print("error")
}
}
publicData.add(saveOperation)
}
func writeImage(image: UIImage) -> URL {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let fileURL = NSURL(fileURLWithPath: documentsURL.absoluteString).appendingPathComponent(".jpg")
if let imageData = image.lowestQualityJPEGNSData {
do {
try imageData.write(to: fileURL!)
} catch {
print("ERRO 001 = \(error.localizedDescription)")
}
}
return fileURL!
}
extension UIImage {
var uncompressedPNGData: Data? { return UIImagePNGRepresentation(self) }
var highestQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 1.0)}
var highQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 0.75) }
var mediumQualityJPEGNSData: Data?{ return UIImageJPEGRepresentation(self, 0.5)}
var lowQualityJPEGNSData: Data? { return UIImageJPEGRepresentation(self, 0.25) }
var lowestQualityJPEGNSData:Data? { return UIImageJPEGRepresentation(self, 0.0)}
}
</code></pre>
<p>如果我只保存字符串,一切都会完美运行,但对于图像,它不会保存记录。</p>
<p>我知道追加可能有任何问题,或者我必须以其他方式保存数组,或者我不应该将其保存为 CKRecordValue。</p>
<p>您对如何实现这一点有任何提示吗?</p>
<p>谢谢</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>当您创建本地 Assets 文件时,您应该使用原子写入选项。这将确保在 CloudKit 尝试上传 Assets 之前文件已完全写入。</p>
<p>这是我在 <a href="https://github.com/paulw11/Seam3" rel="noreferrer noopener nofollow">Seam 3</a> 中使用的 Assets 文件创建功能图书馆:</p>
<pre><code>fileprivate func createAsset(data: Data) -> CKAsset? {
var returnAsset: CKAsset? = nil
let tempStr = ProcessInfo.processInfo.globallyUniqueString
let filename = "\(tempStr)_file.bin"
let baseURL = URL(fileURLWithPath: NSTemporaryDirectory())
let fileURL = baseURL.appendingPathComponent(filename, isDirectory: false)
do {
try data.write(to: fileURL, options: [.atomicWrite])
returnAsset = CKAsset(fileURL: fileURL)
} catch {
print("Error creating asset: \(error)")
}
return returnAsset
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 将 Assets 列表(数组)保存到特定的 CKRecord,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/43357769/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/43357769/
</a>
</p>
页:
[1]