【问题标题】:Firebase storage url for tableview image用于 tableview 图像的 Firebase 存储 url
【发布时间】:2016-12-01 09:41:01
【问题描述】:

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

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

我的应用调用JSON,以便在表格视图中填充文章标题的标签和同一单元格中的图像。

我遇到的问题是检索存储资产的图像 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”块需要更长的时间,所以我要么需要观察什么时候完成,而不是在每篇文章完成获取其图片 URL 之前重新加载我的表格

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

也供参考,我正在使用 SDWebimage 延迟加载图像,但由于上述检索 URL 的延迟,第一次tablereloads 时 URL 目前不存在

提前致谢!

【问题讨论】:

    标签: ios objective-c image firebase firebase-storage


    【解决方案1】:

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

    // 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"]];
    

    【讨论】:

      【解决方案2】:

      根据您的要求,SDWebImage 有一个扩展类,它只引用 FIRStorageReference 并下载您的图像。

      请参见下面的代码:

      FIRStorageReference *storageRef = [[FIRStorage storage]
                                                 referenceWithPath:[NSString stringWithFormat:@"/folder/images/%@",aDictCardData[@"url"]]];
              
              [cell.imgDetail sd_setImageWithStorageReference:storageRef
                                             placeholderImage:nil
                                                   completion:^(UIImage *image,
                                                                NSError *error,
                                                                SDImageCacheType
                                                                cacheType,
                                                                FIRStorageReference *ref) {
                                                       if (error != nil) {
                                                           NSLog(@"Error loading image: %@", error.localizedDescription);
                                                       }
                                                   }];
      

      在上面的代码中,您可以找到sd_setImageWithStorageReference 方法和storageRef 以轻松访问FireBase Image

      您可以从这里找到这个扩展类文件:

      https://github.com/firebase/FirebaseUI-iOS/tree/master/FirebaseStorageUI

      将这些类添加到您的项目中并使用此方法。

      希望这将帮助您在不获取该图像的NSURL 的情况下获取 FireBase 图像。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-10-08
        • 2018-01-05
        • 2017-01-14
        • 1970-01-01
        • 2017-08-10
        • 2018-05-02
        • 1970-01-01
        • 2016-11-20
        相关资源
        最近更新 更多