我正在开发一个与Apache Usergrid协同工作的iOS应用程序。到目前为止,一切都正常,我可以注册用户,登录,查询...
顺便说一句,我正在使用Usergrid 2.1.0版本,在我自己的服务器上运行。
现在我想存储用户的个人资料图片。我是这样做的:
let image = UIImage(named:"user.png")!
let asset = UsergridAsset(fileName: "profilepic", image: image, imageContentType: .Png)!
Usergrid.currentUser!.uploadAsset(asset, progress: nil) { (response, asset, error) -> Void in
if response.ok {
print("Picture saved")
} else {
self.showErrorMessage("Picture couldn't be saved")
print(response.description)
}
}这似乎起作用了,因为当我查看Portal时,我可以在我的用户实体中看到一些关于“文件元数据”的东西。现在的问题是:如何找回图像?我尝试了以下几种方法:
Usergrid.currentUser?.downloadAsset("image/png", progress: nil, completion: { (asset, error) -> Void in
if (error == nil) {
if let image = UIImage(data: (asset?.data)!) {
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.profileImageButton.setImage(image, forState: .Normal)
})
}
}
}但每次我收到错误消息"Entity in not have asset attached“时,实际上我可以在火狐RESTClient的帮助下看到图片。我做错了什么?
发布于 2016-02-25 05:03:31
当entity.hasAsset为false时,也就是当entity.asset == nil && entity.fileMetaData?.contentLength <= 0时,会出现错误"Entity not have a asset attached“。
如果您正在上传资源,然后立即直接下载,则Usergrid.currentUser可能没有更新其fileMetaData或资源实例属性。
在尝试检索资产数据之前,我会尝试通过调用Usergrid.currentUser!.reload()来更新当前用户。
Usergrid.currentUser!.reload() { response in
if response.ok {
Usergrid.currentUser!.downloadAsset("image/png", progress:nil) { downloadedAsset, error in
// Handle downloaded asset here.
}
}
}同样值得注意的是,可以在我的fork here上找到更新的swift sdk (测试版)版本。
https://stackoverflow.com/questions/35586140
复制相似问题