【发布时间】:2016-05-01 16:59:27
【问题描述】:
我有一个图像(UIImage,它也是 url),我试图将它作为 CKAsset 发送到 CloudKit,但我遇到了这个错误:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Non-file 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 会更容易吗?因为,正如我之前所说,我拥有它们!非常感谢任何帮助!谢谢各位!!
【问题讨论】:
-
读取错误。
self.photoURL不是文件 URL。它是什么?打印网址并在您的问题中发布输出。 -
self.photoURL是一个字符串。我使用 Facebook 请求获取用户数据!这是输出:(fbcdn-profile-a.akamaihd.net/hprofile-ak-prn2/v/t1.0-1/p200x200/…) -
如果字符串表示本地文件路径,您需要做的就是使用
fileURLWithPath初始化程序而不是string初始化程序正确创建NSURL。