gpt4 book ai didi

ios - 如何将图像作为 CKAsset 正确发送到 CloudKit?

转载 作者:可可西里 更新时间:2023-11-01 03:27:59 24 4
gpt4 key购买 nike

我有一个图像(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 的唯一方法是:

  1. 将图像临时保存到磁盘
  2. 创建 CKAsset
  3. 删除临时文件

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/36969341/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com