I am trying to upload an UIImage to CloudKit in two steps:
- get a file URL for my UIImage
- get an CKAsset for my file URL
However, when debugging the CKAsset I create in the second step I noticed that the file path to my image doesn't exist and therefore CloudKit is unable to save the asset!
Here is my code:
// 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
}
}
}
Any idea what is going on here? Is this a regression in beta 6 or is my code wrong?
Thanks for feedback!
-- Mark
Here some lldb statements:
(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>