我正在开发一个与 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)
})
}
}
}
但每次我收到错误“实体没有附加 Assets ”时,实际上我可以在 Firefox RESTClient 的帮助下看到图像。我做错了什么?
最佳答案
当 entity.hasAsset
时出现错误“实体没有附加 Assets ”当 entity.asset == nil && entity.fileMetaData?.contentLength <= 0
时又是假的.
如果您正在上传 Assets 并在之后直接下载它,Usergrid.currentUser
可能尚未更新其 fileMetaData 或 Assets 实例属性。
我会尝试通过调用 Usergrid.currentUser!.reload()
来更新当前用户在尝试检索 Assets 数据之前。
Usergrid.currentUser!.reload() { response in
if response.ok {
Usergrid.currentUser!.downloadAsset("image/png", progress:nil) { downloadedAsset, error in
// Handle downloaded asset here.
}
}
}
同样值得注意的是,可以在我的 fork 上找到 swift sdk 的最新(测试版)版本 here .
关于ios - Apache Usergrid Swift SDK 访问 Assets 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35586140/