ios - tableview 图片的 Firebase 存储 url

标签 ios objective-c image firebase firebase-storage

我正在将我的后端从 Parse :'( 迁移到 Firebase,并且在理解 Firebase 围绕 Assets 和引用它们的实现时遇到了一些问题。

本质上,我需要一个 JSON 结构来详细说明一些文章,这些文章中的每一篇都有一张图片。目前 JSON 是手动添加的,以便应用程序引用最新的文章。此图像手动上传到 Firebase 的存储方面,并将其文件名添加到文章的 JSON 中。

我的应用程序调用 JSON 以在同一单元格中使用文章标题标签和图像填充表格 View 。

我遇到的问题是检索存储 Assets 的图像 URL,以便将图像加载到 tableview -

首先我从数据库中调用文章的数据

  FIRDatabaseReference *ref =  [[FIRDatabase database] reference];
    
    
    [[ref child:@"articles"] observeSingleEventOfType:FIRDataEventTypeValue withBlock:^(FIRDataSnapshot * _Nonnull snapshot) {
       
        NSArray *articles = (NSArray *)snapshot.value;} withCancelBlock:^(NSError * _Nonnull error) {
        NSLog(@"%@", error.localizedDescription);
       
    }];

将返回的快照转换为模型对象——其中文章标题设置为模型对象以及生成的图像 URL

Article *article = [Article new];
    article.title = [articleObj valueForKey:@"title"];
    if ([articleObj objectForKey:@"image"] != [NSNull null]) {

    // Points to the root reference
    FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com"];
    // Points to "images"
    FIRStorageReference *imagesRef = [storageRef child:@"articleImages"];
    
    // Note that you can use variables to create child values
    FIRStorageReference *imagePath = [imagesRef child:articleObj[@"image"]];

    if (imagePath){
        article.imageURL = [NSURL URLWithString:imagePath.fullPath];

    // Fetch the download URL
    [imagePath downloadURLWithCompletion:^(NSURL *URL, NSError *error){
        if (error != nil) {
            // Handle any errors
            NSLog(@"**ERROR %@", error.description);
            NSLog(@"**ERROR %@", URL.absoluteString);
            NSLog(@"**ERROR %@",article.title);
        } else {
            article.imageURL = URL;

        }
    }];
    }
    

我对这种方法的问题(除了它感觉像很多代码,感觉它应该是一个标准属性)是文章标题立即可用,但 'downloadURL' block 需要更长的时间,所以我要么需要观察它何时完成,要么在每篇文章完成获取其图像 URL 之前重新加载我的表

所以问题是: 是否有替代的非阻塞方式来获取图像 URL 或者我在存储门户中看到的 URL 引用是否足够可靠以在我的 JSON 中使用而不是对图像名称的引用(因此将 URL 存储在我的数据库中而不是图像名称)

另外作为引用,我正在使用 SDWebimage 来延迟加载图像,但是由于上述检索 URL 的延迟,该 URL 目前在第一次 tablereloads 时不存在

提前致谢!

最佳答案

您可以通过替换这些将三行代码转换为一行,

// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com"];
// Points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"articleImages"];   
// Note that you can use variables to create child values
FIRStorageReference *imagePath = [imagesRef child:articleObj[@"image"]];

// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] referenceForURL:@"gs://project-3229518851181635221.appspot.com/articleImages/image"]];

关于ios - tableview 图片的 Firebase 存储 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38611932/

相关文章:

ruby-on-rails - 我应该使用什么 Ruby on Rails 图像库来制作词汇卡?

c++ - boost:python 传递一个指向指针的指针作为参数

python - 获取图像大小而不将图像加载到内存中

ios - 从不同的 View Controller 访问 textField.text 变量

ios - 如何在 ScrollView 中添加所有(标签,按钮, subview )?

objective-c - 远程调试 iOS 性能

ios - Estimote iBeacons 未检测到

ios - 在 Swift 中使用标签

ios - 如何禁用 TestFlight Beta 测试?

ios - 算术运算符和键值编码