【发布时间】:2016-06-05 19:08:42
【问题描述】:
我正在开发一个与 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)
})
}
}
}
但每次我得到错误“实体没有附加资产”时,实际上我可以在 Firefox RESTClient 的帮助下看到图像。我做错了什么?
【问题讨论】:
-
会不会是 Swift SDK 出了什么问题?当我构建自己的 UsergridRequest,手动设置要检索的文件的路径和标题时,我确实收到了带有图像数据的回复,这当然会引发错误,因为它无法转换为 JSON。试图找出错误...
标签: ios swift sdk assets usergrid