作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个图像(UIImage 和它的 url),我试图将它作为 CKAsset 发送到 CloudKit,但我遇到了这个错误:Terminating app due to uncaught exception '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/
我是一名优秀的程序员,十分优秀!