我有一个图像(UIImage,它也是 url),我试图将它作为 CKAsset 发送到 CloudKit,但我遇到了这个错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因: '非文件 URL'
。代码如下:
override func viewDidLoad() {
super.viewDidLoad()
send2Cloud()
}
func send2Cloud() {
let newUser = CKRecord(recordType: "User")
let url = NSURL(string: self.photoURL)
let asset = CKAsset(fileURL: url!)
newUser["name"] = self.name
newUser["photo"] = asset
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(newUser, completionHandler: { (record: CKRecord?, error: NSError?) in
if error == nil {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
print("User saved")
})
} else {
print(error?.localizedDescription)
}
})
}
我有 URL,我可以打印它,复制并粘贴到我的导航器,它会显示我的图像!所以,我不知道这里发生了什么......
如果我使用 UIImage 而不是它的 URL 会更容易吗?因为,正如我之前所说,我拥有它们!非常感谢任何帮助!谢谢各位!!
根据我的经验,将上传 UIImage
保存为 CKAsset
的唯一方法是:
let data = UIImagePNGRepresentation(myImage); // UIImage -> NSData, see also UIImageJPEGRepresentation
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).URLByAppendingPathComponent(NSUUID().UUIDString+".dat")
do {
try data!.writeToURL(url, options: [])
} catch let e as NSError {
print("Error! \(e)");
return
}
newUser["photo"] = CKAsset(fileURL: url)
// ...
publicData.saveRecord(newUser, completionHandler: { (record: CKRecord?, error: NSError?) in
// Delete the temporary file
do { try NSFileManager.defaultManager().removeItemAtURL(url) }
catch let e { print("Error deleting temp file: \(e)") }
// ...
}
NSData
初始化 CKAsset
,但还没有完成。
关于ios - 如何正确地将图像作为 CKAsset 发送到 CloudKit?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45573269/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |