我试图通过两个步骤将UIImage上传到CloudKit:
然而,当调试我在第二步创建的CKAsset时,我注意到我的图像的文件路径不存在,因此CloudKit无法保存资产!
这是我的代码:
// thumbnail is my UIImage
if let thumbnailURL = thumbnail.urlOf() {
self.thumbnailAsset = CKAsset(fileURL: thumbnailURL)
self.record["thumbnail"] = thumbnailAsset
} else {
NSLog("Could not create thumbnail asset!")
}
extension UIImage {
func urlOf() -> URL? {
do {
let imageData = UIImageJPEGRepresentation(self, 1)
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let documentDirectory = paths[0] as String
let myFilePath = NSURL(fileURLWithPath: documentDirectory).appendingPathComponent("thumbnail.jpg")
try imageData?.write(to: myFilePath!, options: .atomicWrite)
let url = myFilePath! as URL
return url
} catch {
NSLog("Could not save thumbnail to local directory!")
return nil
}
}
}
知道这里发生了什么吗?这是beta 6中的回归还是我的代码错了?
感谢您的反馈!
- 马克
这里有一些lldb陈述:
(lldb) po thumbnailURL
▿ file:///Volumes/Work/Users/mark/Library/Developer/CoreSimulator/Devices/B1F9E45D-2A75-454F-AC56-37186428E833/data/Containers/Data/Application/E02EE6DF-17E4-4809-8978-339E76ED1EB3/Documents/picture
(lldb) po self.record["thumbnail"]
▿ Optional<CKRecordValue>
(lldb) po self.record["thumbnail"]!
<CKAsset: 0x6100001e8400; path=~/Documents/picture, UUID=BF838CB7-E828-4CD8-8517-911E25818088>