ios - Apache Usergrid Swift SDK 访问 Assets 图像

标签 ios swift sdk assets usergrid

我正在开发一个与 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/

相关文章:

swift - 如何将约束转换为 Item 或 Item 从 viewA 到 viewB?我收到此错误 : Unknown layout attribute

swift - 如何从两个实体核心数据获取数据并在 TableView 中显示(Swift)

iphone - 如何在 iPhone 上正确处理 HTTP 摘要认证

iphone - 如何退回 iPhone 键盘

ios - UITextView 中的链接在 Swift 4 中不起作用

string - 如何找到 Swift 字符串中最后一次出现的子字符串?

iphone - 如何在Xcode中用+替换空格

iphone - NSString——静态的还是内联的?有任何性能提升吗?

ios - iOS CoreData 中的孤立对象

android - 为什么 Android Studio 安装程序会将 Android SDK 放入 %USER% 文件夹中?